write matrix, appending data

조회 수: 36 (최근 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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by