write matrix, appending data

37 ビュー (過去 30 日間)
Birsen Ayaz-Maierhafer
Birsen Ayaz-Maierhafer 2024 年 4 月 9 日
編集済み: Kevin Holly 2024 年 4 月 9 日
Hi,
I have some data I want to pass to excel file
C is the titles and filename is my excel file, for example filename=myexcelfile.xlsx. I wrote the following data and worked.
writecell(C,filename,'Sheet',Tag,'Range','A1'); %write the headers
writematrix(ppm_data', filename,'Sheet',Tag,'Range','A2:A9')
writematrix(data_k, filename,'Sheet',Tag,'Range','B2')
writematrix(data_b, filename,'Sheet',Tag,'Range','D',nextI);
writematrix( delta,filename,'Sheet',Tag,'Range','F',nextI2); format shortEng
However I am going through a loop and want to append the data 10 lines after the previous dump in the same sheet. I came up with following code:
nextI=2; format shortEng
nextI2=3; format shortEng.
. .
for ....
writecell([C,filename,'Sheet',Tag,'Range','A',nextI-1]);
writematrix([ppm_data', filename,'Sheet',Tag,,'Range','A',nextI,':A',nextI+7])
writematrix(data_k, filename,'Sheet',Tag,'Range','B',nextI');
writematrix(data_b, filename,'Sheet',Tag,'Range','D',nextIsotope);
writematrix( delta,filename,'Sheet',Tag,'Range','F',nextIsotope2); format shortEng
nextI=nextI + 10
nextI2=nextI2 + 10
end
Did not work. And here is the error
writematrix([ppm_data', filename,'Sheet',Tag,'Range','A',nextI,':A',nextI+7])
Dimensions of arrays being concatenated are not consistent.
Does anyone knows how to resolve this issue?
Thank you

採用された回答

Voss
Voss 2024 年 4 月 9 日
Instead of
writecell([C,filename,'Sheet',Tag,'Range','A',nextI-1]);
writematrix([ppm_data', filename,'Sheet',Tag,,'Range','A',nextI,':A',nextI+7])
writematrix(data_k, filename,'Sheet',Tag,'Range','B',nextI');
writematrix(data_b, filename,'Sheet',Tag,'Range','D',nextIsotope);
writematrix( delta,filename,'Sheet',Tag,'Range','F',nextIsotope2);
try
writecell(C,filename,'Sheet',Tag,'Range',"A"+(nextI-1));
writematrix(ppm_data', filename,'Sheet',Tag,'Range',"A"+nextI+":A"+(nextI+7))
writematrix(data_k, filename,'Sheet',Tag,'Range',"B"+nextI);
writematrix(data_b, filename,'Sheet',Tag,'Range',"D"+nextIsotope); % should nextIsotope be nextI?
writematrix( delta,filename,'Sheet',Tag,'Range',"F"+nextIsotope2); % should nextIsotope2 be nextI2?

その他の回答 (1 件)

Kevin Holly
Kevin Holly 2024 年 4 月 9 日
編集済み: Kevin Holly 2024 年 4 月 9 日
ppm_data = rand(1,10);
filename = 'myexcelfile.xlsx';
Tag = 'Sheetname';
nextI=2; format shortEng
nextI2=3; format shortEng
The error occurs when concatenating (combining) the following:
[ppm_data', filename,'Sheet',Tag,'Range','A',nextI,':A',nextI+7]
I believe you desire to just concatenate the following (note, you need to convert the numeric values to a character array format using the num2str function):
['A',num2str(nextI),':A',num2str(nextI)+7]
ans = 'A2:A9'
All together you would obtain:
writematrix(ppm_data', filename,'Sheet',Tag,'Range',['A',num2str(nextI),':A',num2str(nextI+7)])
Also
writecell([C,filename,'Sheet',Tag,'Range','A',nextI-1]);
should be
writecell(C,filename,'Sheet',Tag,'Range',['A',num2str(nextI-1)]);

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by