num2str in for loop assignment
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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.
0 个评论
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');
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!