I want to save a figure to file with a variable file name (composed from a data group and a number). The following doesn't work because matlab reads 'filename' as a literal not a variable.
print '-pdf' filename
The documentation says to use the functional form and gives the following as an example:
print('-dpdf', filename)
Using this generates the matlab error
"??? Error using ==> print at 325
Handle input argument contains non-handle value(s).
Error in ==> printtest at 103
print('-dpdf', filename)"
Adding the handle reference 'gcf' (with or without quotes) gets the same error.
This one works in R2006a:
plot(rand(10))
fname = 'test.pdf';
print(fname,'-dpdf')
If the example you state is verbatim from the docs, the
docs are flawed.
Rune
I tried your suggestion but it didn't work. I am running 2009b which, to be honest, sucks badly compared to earlier versions. Here's a quote from the documentation:
"This example combines the previous two examples and uses the function form to print using both a handle variable and a file name variable:
h = figure; plot(1:4,5:8)
filename = 'mydata';
print(h, '-dpsc', filename)
This example works ONLY if you assign filename the literal value 'x'. If you try to assign it a text variable, the command fails with a reference to the handle.
I've also been disappointed with changes to plotting functions such as bar plots, which used to be fast and simple to get nice results just using the defaults. Text concatenation doesn't seem to work the way it used to. Documentation is badly written and unclear. My "outsource" detector is firing.
Rune Allnor <all...@tele.ntnu.no> wrote in message <51336523-3117-426c...@s12g2000yqj.googlegroups.com>...
I'm not sure I undesrand: Matlab saves the file under tha *name*,
not the contents, of the variable?
Weird.
This one works under 2006a:
h = figure;
plot(1:4,5:8)
filename = sprintf('f%04d-%02d.pdf',1,2);
print(h, '-dpdf', filename)
Rune
your example works flawlessly on this r2009b(!)...
what does this yield in your system
which print -all;
us
This works:
h = figure(10)
plot(x,y)
fname = 'Bingo'
print(h, '-dpdf', fname)
>> fname
fname =
Bingo
And this also works:
h = figure(10)
plot(x,y)
filename = 'Bingo'
fname = filename;
print(h, '-dpdf', fname)
>> fname
fname =
Bingo
BUT THIS DOESN'T WORK:
h = figure(10)
plot(x,y)
namelist = textread('testfile','%s');
fname = namelist(1)
print(h, '-dpdf', fname)
>> fname
fname =
'Bingo'
So my text variable is wrong but I'm not sure how to fix it.
Thanks again,
Helen
"us " <u...@neurol.unizh.ch> wrote in message <hkkajd$jmp$1...@fred.mathworks.com>...
> BUT THIS DOESN'T WORK:
> h = figure(10)
> plot(x,y)
> namelist = textread('testfile','%s');
> fname = namelist(1)
> print(h, '-dpdf', fname)
namelist will be a cell array, so namelist(1) will be a 1x1 cell array.
If you want the contents of that array (the string), you need
fname = namelist{1}
ImageAnalyst <imagea...@mailinator.com> wrote in message <0bb51d64-ee63-4d81...@k22g2000vbp.googlegroups.com>...
Regards,
Helen
Walter Roberson <robe...@hushmail.com> wrote in message <hkl8o4$qkd$1...@canopus.cc.umanitoba.ca>...
That is correct. This is known as command-function duality -- you can call
most functions as though they were commands.
> The documentation says to use the functional form and gives the following
> as an example:
> print('-dpdf', filename)
>
> Using this generates the matlab error
>
> "??? Error using ==> print at 325
> Handle input argument contains non-handle value(s).
>
> Error in ==> printtest at 103
> print('-dpdf', filename)"
>
> Adding the handle reference 'gcf' (with or without quotes) gets the same
> error.
Looking at some of the later threads, I think I know what the problem is.
Set a breakpoint on line 103 of your printtest script/function (click on the
little line to the right of the line number in the Editor; you should see a
red dot appear on that little line.) Then run printtest, and when you see
the green arrow on that line go to the Command Window and type "whos
filename".
If you see the word "cell" in the Class column, then the problem is that
PRINT expects its inputs to be strings -- char arrays -- but you're passing
it a cell array containing a string. In that case, replace the line of
code:
print('-dpdf', filename)
with:
if iscell(filename)
print('-dpdf', filename{1});
else
print('-dpdf', filename);
end
That will call PRINT with the contents of the filename variable (if it is a
char array) or the contents of the first cell of the filename variable (if
it is a cell array.)
In any case, PRINT should display an error message that more clearly
indicates the cause of the problem when you pass a cell array in -- I will
enter that into our bug database.
--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
I am sorry to hear you feel that way. Are there particular problems (aside
from this one) that you've run into? If so, you should send your feedback
to Technical Support and/or your sales representative.
> Here's a quote from the documentation:
> "This example combines the previous two examples and uses the function
> form to print using both a handle variable and a file name variable:
>
> h = figure; plot(1:4,5:8)
> filename = 'mydata';
> print(h, '-dpsc', filename)
>
> This example works ONLY if you assign filename the literal value 'x'. If
> you try to assign it a text variable, the command fails with a reference
> to the handle.
> I've also been disappointed with changes to plotting functions such as bar
> plots, which used to be fast and simple to get nice results just using the
> defaults. Text concatenation doesn't seem to work the way it used to.
> Documentation is badly written and unclear. My "outsource" detector is
> firing.
I can assure you, we're not outsourcing. We do sometimes make changes that
we hope make our products more powerful, more usable, and/or more efficient
(and that sometimes is a trade-off for one of those considerations against
the others) -- again, if you feel that we haven't succeeded, please give us
your feedback on what you feel we should improve (and what you feel we've
done well.)
More than likely, in your situation, it's being returned as a cell array due
to the format of your file and/or the format specifier you're passing into
TEXTREAD.
> I can no longer count the number of 2007 scripts that are now broken.
> What happened to backward compatibility?
Backwards compatibility is still extremely important. If something that we
changed that broke your work, TELL US so that when or if we're considering
making a similar change in the future, we can take into account the fact
that we once broke backwards compatibility with a similar change when
determine whether to make thte change or how to inform users about the
change.
> For 12 years matlab worked easily and flawlessly over a wide range of
> conditions and purposes. Now it seems everything is broken. Wow.
> They even got rid of "nanmean". Or maybe I have to buy the $1000 stats
> package to get it? Sign me, "disappointed in Palo Alto".
NANMEAN is and has always been (well, at least as far back as I'm aware)
part of Statistics Toolbox.
http://www.mathworks.com/access/helpdesk/help/toolbox/stats/nanmean.html
The earliest reference that I can find to nanmean in this newsgroup is
December 27 2007, and it was referenced as being part of the stats toolbox then.
It appears you can get a free version of nanmean et al. from the Matlab File
Exchange,
http://www.mathworks.com/matlabcentral/fileexchange/6837
I haven't been able to trace it back any further, but the current behaviour of
textread() of producing a cell array of strings when the format element is
"%s", existed at least as early as Release 13 Service Pack 2 (6.5.2)
http://www.mathworks.com/access/helpdesk_r13/help/techdoc/ref/textread.html
textread() is not one of the functions listed as having changed between 6.1
and 6.5
http://www.mathworks.com/access/helpdesk_r13/help/base/relnotes/matlab/matlab1312.html
Release 6.1 was released in mid December 2001, more than 8 years ago, and 5
years before you started writing those 2007 scripts.
Possibly you have been accustomed to using the '%c' format element instead of
the '%s' format element.
My print command is now working and I appreciate everyone's helpful responses.
Regards,
helen
Walter Roberson <robe...@hushmail.com> wrote in message <hkprql$imm$1...@canopus.cc.umanitoba.ca>...
Now if you are examining your old scripts you might want to get rid of all textread functions. Documentation of textread has suggested using textscan instead for years now, moving to more serious form of warning in each release.
In 2008b:
Note The textscan function is intended as a replacement for both textread and strread.
And in 2010a prerelease:
Note textread will be removed in a future version. Use textscan instead.
Thanks! I really can find such a basic example everywhere!
"Helen " <helenf...@gmail.com> wrote in message <hkqihe$h6a$1...@fred.mathworks.com>...
Then do something like this (untested)
myFolder = 'C:\Documents and Settings\yourUserName\My Documents\My
Pictures';
for k = 1 : numberOfFiguresYouHave
h = figure(k); % Activate this figure.
baseFileName = sprintf('Figure %d.BMP', k);
fullFileName = fullfile(myFolder, baseFileName);
export_fig(fullFileName , h);
end
You may need to adapt the above.