How do I stop a function from displaying output matrix without using semicolon in command window?
Show older comments
Here is one of my functions for color histogram equalization:
function [ newimg ] = oldhist( img );
Red = img(:,:,1);
Green = img(:,:,2);
Blue = img(:,:,3);
R = histeq(Red);
G = histeq(Green);
B = histeq(Blue);
newimg= cat(3,R,G,B);
imshow (newimg);
end
If I use it without a semicolon in the command window it will display the newimg matrix. How do I store newimg matrix without displaying the matrix and without using a semicolon in the command window?
2 Comments
Shashank Prasanna
on 12 Mar 2013
Why don't you want to use the semicolon? It exists so that you can suppress command line output.
James
on 13 Mar 2013
Accepted Answer
More Answers (1)
Walter Roberson
on 12 Mar 2013
When you call a function that returns a value, the value will be displayed unless the context specifically instructs otherwise (such as with a semi-colon), or unless the context uses the returned value in a calculation in a way that does not display the evaluated value. For example,
if oldhist( img )
end
will use the value output by oldhist() to test for the "if" and will not display the value. Note this particular multi-line form does not use a semi-colon.
It is not possible in a MATLAB routine to find out the name of an output variable.
You could recode as
function oldhist(img, newimgvar )
....
assignin('caller', newimgvar, newimg);
where newimgvar is a string indicating the name of the variable to assign into. For example,
oldhist( eye(5), 'MyResult' )
This is not recommended and will fail if the calling workspace is a static workspace (that is, there is an "end" statement matching its "function" statement.)
If you were hoping for something like a command
displayoutputs off
then there is no such command in MATLAB.
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!