问题
I have been trying to write a simple script in MATLAB that plots an image, asks to user if they want to print it to a file, and (if yes) does this. However, I have met a strange error with the print()
function. Here is my code:
plot(X, Y, 'red');
choice = input('Do you want to print to file this 2D image ? [y/n] ', 'y');
if(choice=='Y' || choice=='y')
{
print(hFig, '-dpng', strcat(filename, '.png'));
}
If running, it stops inside the if
statement with the error:
Error in ==> print at 161 err.message='';
??? Output argument "varargout" (and maybe others) not assigned during call to "C:\Programmi\MATLAB\R2010a\toolbox\matlab\graphics\print.m>print".
Error in ==> istogramma at 30 print(hFig, '-dpng', strcat(filename, '.png'));
Why am I getting this error and how can I avoid this?
回答1:
Your if
code with {
and }
seems strange, in MATLAB {
and }
are used for cell arrays and cell array indexing, not for code structuring. Further, the second argument for input
must be 's'
, not 'y'
as you have.
Fixed code:
choice = input('Do you want to print to file this 2D image ? [y/n] ', 's');
if(choice=='Y' || choice=='y')
print(hFig, '-dpng', strcat(filename, '.png'));
end
Edit: to keep asking until user responds with 'y', 'Y', 'n' or 'N':
choice = '';
while ~ismember(choice, {'y', 'Y', 'n', 'N'})
choice = input('Do you want to print to file this 2D image ? [y/n] ', 's');
end
if(choice=='Y' || choice=='y')
print(hFig, '-dpng', strcat(filename, '.png'));
end
回答2:
So confusing! Because print command dont have any output argument!!!
I dont know clearly, but I think first of all check the hFig assignment. you can use
hFig=figure;
plot(X, Y, 'red');
% ...
if you want to create a figure and draw anything you want in it. as the error says your output argument is not assigned so check the "filename" or you can use
[filename '.png']
instead.
I hope this help you. right now I dont have MATLAB and I cant test it for you.
P.S. : See this : Why do I get the error '??? Output argument ‹variable› (and maybe others) not assigned during call to ‹function›.' ?
来源:https://stackoverflow.com/questions/10634678/error-with-print-output-argument-varargout-and-maybe-others-not-assigned