Variable file name in print function using object handle

I am trying to save a file using its handle. The name of the file is variable. How do I do it using the print function?
My attempt:
for i = 1:100
var = int2str(i);
z = strcat(var,'.bmp');
I = imread(z);
imhist(I);
h = gcf;
print(h, '-dbitmap' , 'D:\Figures\z');
end
I get a file named z.bmp instead of the string. How do I change the code to save the file as a string contained in z?

Risposte (1)

dpb
dpb il 7 Giu 2014
Modificato: dpb il 8 Giu 2014
The problem is you wrote the whole name as a literal string instead of using the variable--
print(h, '-dbitmap' , fullfile('D:\Figures',z));
NB: that as you have it written, your filenames will be just '1.bmp', '2.bmp', etc., ... That may be ok and what you intend but you might consider a little more sophistication in naming as sotoo
print(h, '-dbitmap' , fullfile('D:\Figures',sprintf('Fig%02d',i)));
This will produce 'Fig01', 'Fig02', etc., ... which will sort alphabetically numerically as well when get to double-digits and will also identify figure files as figures besides the extension (which Matlab automagically supplies according to the type specified so don't need to create it explicitly). Just a suggestion...

Categorie

Tag

Richiesto:

il 7 Giu 2014

Modificato:

dpb
il 8 Giu 2014

Community Treasure Hunt

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

Start Hunting!

Translated by