Variable file name in print function using object handle

2 views (last 30 days)
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?

Answers (1)

dpb
dpb on 7 Jun 2014
Edited: dpb on 8 Jun 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...

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!