How to overwrite labels and data in an excel file?
11 次查看(过去 30 天)
显示 更早的评论
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?
回答(1 个)
Ameer Hamza
2020-10-16
编辑:Ameer Hamza
2020-10-16
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 个评论
Sadiq Akbar
2020-10-16
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
2020-10-16
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
2020-10-17
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
2020-10-17
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
2020-10-17
编辑:Walter Roberson
2020-10-17
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
2020-10-17
Modified variable name warning is just a warning.
You are not writing the data variable to a file.
Sadiq Akbar
2020-10-17
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
2020-10-17
编辑:Ameer Hamza
2020-10-17
@Sadiq, you can use writetable()
writetable(data, 'filename.xlsx')
Sadiq Akbar
2020-10-17
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
2020-10-17
I am not sure about the issues. These commands work fine in R2020b. The output file I get is attached.
Sadiq Akbar
2020-10-17
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
2020-10-18
https://www.mathworks.com/matlabcentral/answers/617363-how-to-get-a-cdf-plot-of-my-data
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)