num2str in for loop assignment

조회 수: 9(최근 30일)
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
save output_1.mat x y
save output_2.mat x y
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;
save output_num2str(i).mat x o
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


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');


Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by