You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to overwrite labels and data in an excel file?
11 views (last 30 days)
Show older comments
If I have an Excel file having labels in columns A1,B1,C1,D1,E1,F1,G1,H1,I1 and J1. Below the labels are numeric data. Now if I want to overwrite the labels in B1,E1,G1. Also if I want to overwrite the numeric data below E1 and G1, how will I do that?
Answers (1)
Ameer Hamza
on 16 Oct 2020
Edited: Ameer Hamza
on 16 Oct 2020
All of the following functions for writing to excel sheet support 'range' option for writing to excel file
You can specify the range like B1:B100 to replace the first 100 cells of column B in the excel sheet.
14 Comments
Sadiq Akbar
on 16 Oct 2020
Thank you very much Ameer Hamza for your consistant help. I visited the site but again it seems too technical for me. I am attaching an excel file.
Suppose I want to overwrite the label fmin in column C with functionminumum. Likewise I want to overwrite Error in Amplitude1 with EA1, Amplitudes MSE with MSE of Amplitudes.
Further, I want to overwrite data in Column C, F,M with any random data.So how will I do that via Matlab code?
Ameer Hamza
on 16 Oct 2020
Here is an easy method for your excel file
data = readtable('myfile2sn01.xlsx', 'PreserveVariableName', true);
data.Properties.VariableNames{3} = 'functionminumum';
data.Properties.VariableNames{6} = 'EA1';
data.Properties.VariableNames{13} = 'MSE of Amplitudes';
data{:, 3} = rand(size(data,1), 1);
data{:, 6} = rand(size(data,1), 1);
data{:, 13} = rand(size(data,1), 1);
Sadiq Akbar
on 17 Oct 2020
Thank you very much Ameer Hamza for your devoted support and help. I ran the above program, but it gives me the following Error:
>> Excel_Overwrite
Error using readtable (line 197)
Invalid parameter name: PreserveVariableName.
Error in Excel_Overwrite (line 1)
data = readtable('myfile2sn01.xlsx', 'PreserveVariableName', true);
>>
Walter Roberson
on 17 Oct 2020
Leave out 'PreserveVariableName', true in your release.
Also the line
data.Properties.VariableNames{13} = 'MSE of Amplitudes';
will have to be changed to assign something that is a valid MATLAB variable name.
If you need 'MSE of Amplitudes' complete with space to be written into the file then we will need to take a bit different approach.
Sadiq Akbar
on 17 Oct 2020
Edited: Walter Roberson
on 17 Oct 2020
Thank you very much Walter Roberson for your tip. I changed the above program. This time it ran but the desired labels and data were not overwritten. Further Matlab gave me a warning message as:
>> Excel_Overwrite
Warning: Variable names were modified to make them valid MATLAB identifiers. The original
names are saved in the VariableDescriptions property.
>>
The chnaged program is as below:
data = readtable('myfile2sn01.xlsx');
data.Properties.VariableNames{3} = 'functionminumum';
data.Properties.VariableNames{6} = 'EA1';
data.Properties.VariableNames{13} = 'MSE_of_Amplitudes';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Overwrite Data in Columns 3,6 and 13
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data{:, 3} = rand(size(data,1), 1);
data{:, 6} = rand(size(data,1), 1);
data{:, 13} = rand(size(data,1), 1);
Walter Roberson
on 17 Oct 2020
Modified variable name warning is just a warning.
You are not writing the data variable to a file.
Sadiq Akbar
on 17 Oct 2020
Thank you very much Walter Roberson. I am not too much technical. How will we write it to the file, "myfile2sn01.xlsx" , I have given in the attachment? Can you guide me further?
Ameer Hamza
on 17 Oct 2020
Edited: Ameer Hamza
on 17 Oct 2020
@Sadiq, you can use writetable()
writetable(data, 'filename.xlsx')
Sadiq Akbar
on 17 Oct 2020
Thank you very much dear Ameer Hamza for your consistant help. Yes, it worked for the labels. But it didn't work for the numerical data. Further, it filled column "K" data from row33 till end with hashes i.e. ####.
I am attaching the file for your observation.
Ameer Hamza
on 17 Oct 2020
I am not sure about the issues. These commands work fine in R2020b. The output file I get is attached.
Sadiq Akbar
on 17 Oct 2020
Thank you very much dear Ameer Hamza. Indeed you are a true helper. I am very happy from you. Ok I will try to arrange Matlab 2020b if I could. But currently I am not having it. Its expensive too. Thank you very much once again. May you live long to keep helping the needy like me.
Sadiq Akbar
on 18 Oct 2020
https://www.mathworks.com/matlabcentral/answers/617363-how-to-get-a-cdf-plot-of-my-data
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)