num2str in for loop assignment

조회 수: 5 (최근 30일)
friet
friet 2018년 2월 9일
편집: Image Analyst 2018년 2월 9일
I have a matrix output(100,100) imported in matlab. I would like to creat a vector that saves every 10 col. forexample
Output_1=output(:,1);
save output_1.mat x y
Output_2=output(:,10);
save output_2.mat x y
Output_3=output(:,30);
save output_3.mat x y
However, instead of doing so, I would like to create a for loop and this is how I try;
for i= 1:10:100;
Output_num2str(i)=output(:,i);
save output_num2str(i).mat x o
end
and matlab says In an assignment A(:) = B, the number of elements in A and B must be the same.
how can I fix this. Thanks

답변 (2개)

Walter Roberson
Walter Roberson 2018년 2월 9일
In your case:
basename = sprintf('output_%d', i);
clear out_struct
out_struct.(basename) = output(:,i);
out_struct.x = x;
out_struct.y = y;
filename = [basename '.mat']
save(filename, 'out_struct', '-structure');
... but it would be better to use the same variable name in each file instead of using dynamic variable names.

Image Analyst
Image Analyst 2018년 2월 9일
편집: Image Analyst 2018년 2월 9일
You can't do
save output_num2str(i).mat x o
Try this:
thisColumn = output(:,i);
filename = sprintf('output_%d.mat', i);
filename = fullfile(pwd, filename);
save(filename, 'thisColumn', 'x', 'y');

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by