num2str in for loop assignment

4 views (last 30 days)
friet
friet on 9 Feb 2018
Edited: Image Analyst on 9 Feb 2018
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

Answers (2)

Walter Roberson
Walter Roberson on 9 Feb 2018
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 on 9 Feb 2018
Edited: Image Analyst on 9 Feb 2018
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');

Categories

Find more on Entering Commands 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!