John Yates
John Yates

Reputation: 23

How to save graph according to naming array in MATLAB, all from command line?

I have an array in the form of

a = {'a','b',...'t'};

b = {'1','2',....'20'};

I'm doing stuff to plot my data sets (I have 20 of them) via loop, and I want to save my figures like so:

a1.jpg

b2.jpg

.

.

.

for all 20 data sets.

So, my configuration is in the form of

for i = 1:10


*do stuff to get plot*
save(....)???

I don't know how to do this. I have a h=figure; term, and I was expecting to do something along the lines of

save(h,'dataset_a(i)_b(i),'jpg')

However, the naming has to exploit the entries of my 'a' and 'b' array. How could I formally add string entries like so from arrays?

Upvotes: 1

Views: 51

Answers (2)

scmg
scmg

Reputation: 1894

You must create a string for filename to save:

for i = 1:10
  h = plot(); % anything to plot here
  fname = strcat('dataset_', a{i}, '_', b{i});
  saveas(h, fname, 'jpg');
end

Note that you have to use saveas instead of save. Using save cannot give you output format JPEG even if you used .jpg in the file name (it creates a .jpg file but cannot be opened).

Upvotes: 2

user1543042
user1543042

Reputation: 3440

Almost

save(h,['dataset_', a{i}, '_', b{i},'.jpg'])

Upvotes: 2

Related Questions