num2str in for loop assignment
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (2)
Walter Roberson
il 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.
0 Commenti
Image Analyst
il 9 Feb 2018
Modificato: Image Analyst
il 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');
0 Commenti
Vedere anche
Categorie
Scopri di più su Entering Commands in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!