How do I stop a function from displaying output matrix without using semicolon in command window?

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

Why don't you want to use the semicolon? It exists so that you can suppress command line output.
I thought the professor wanted the out put just to be the image not the matrix. I talked to him today and he said what everyone here has, that just putting a semicolon after it will work.

댓글을 달려면 로그인하십시오.

 채택된 답변

Try using
function varargout =
instead of
function newimg =

댓글 수: 4

Then it will not return newimg to the calling function.
You can do the following in the code if you want to return newimg
if nargout
varargout{1} = newimg;
end
Then unless you specify the output, it won't display.
True. But that will not store newimg as required by the user.
I interpret the question as requesting that
var = oldhist( TheImageArray )
must store into var but must not display the result, as if a semi-colon had been given after the command.
I do not know why the user requests this. My best guess at the moment is that the user wants to be able to turn off output to the command window without editing the source of the calling routine.
Oh I see, then I understand the question wrong. In that case I don't know the answer and adding a ; seems to be the best solution for me.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

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.

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

질문:

2013년 3월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by