Mark Hughes
Mark Hughes

Reputation: 245

MATLAB Passing variable name into save function

I'm trying to use a variable name as the filename for a PDF output. The save function works but only when the filename is contained in ''. So... saveas(h, 'fname', 'pdf') works, but I want the value of the variable fname as the filename, not just the text 'fname'. I have tried it without the '', but that simply doesn't save anything.

Thanks.

Upvotes: 1

Views: 2637

Answers (1)

Acorbe
Acorbe

Reputation: 8391

This works fine on my machine (R2012b winx64) and generates the two expected pdfs

xx = 0:.01:1
h = plot(xx,sin(xx))

f_name = 'fname'  % // string version
saveas(h, f_name , 'pdf')  % // PDF1 - name: "fname.pdf"

f_name = 10;  % // now variable is a double
saveas(h, num2str(f_name) , 'pdf')  % // convert number in string first
                                    % // PDF2 - name "10.pdf"

Upvotes: 2

Related Questions