write matrix, appending data

36 views (last 30 days)
Birsen Ayaz-Maierhafer
Birsen Ayaz-Maierhafer on 9 Apr 2024
Edited: Kevin Holly on 9 Apr 2024
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

Accepted Answer

Voss
Voss on 9 Apr 2024
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?

More Answers (1)

Kevin Holly
Kevin Holly on 9 Apr 2024
Edited: Kevin Holly on 9 Apr 2024
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)]);

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!