How can I check for semicolon termination of my own function

My function does something and then writes information to the screen by using
function a = say_hi(b)
a = b;
fprintf(1, 'Hello World');
end
I would like to check if the line in the script that called this function was terminated by a semicolon, such that merely does a = b in that case. Is that possible?
Kind regards,
Jan

댓글 수: 1

OP said in answers instead of comments:
---
Thanks Walter,
I understand what you say. Let me rephrase the question. I could do what I want by defining the function as follows:
function a = say_hi(b, print)
a = b;
if (print == 1)
fprintf(1, 'Hello World');
end
end
But then I would have an additional input parameter, which is undesired. I am looking for a keyword such as 'nargin' that is available inside the function space to check for its termination i.e. '\n' or ';\n'.

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

 채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 21일

0 개 추천

Semi-colon does not suppress anything explicitly printed: it only affects whether the value of assignments are automatically printed.

댓글 수: 4

Thanks Walter,
I understand what you say. Let me rephrase the question. I could do what I want by defining the function as follows:
function a = say_hi(b, print)
a = b;
if (print == 1)
fprintf(1, 'Hello World');
end
end
But then I would have an additional input parameter, which is undesired. I am looking for a keyword such as 'nargin' that is available inside the function space to check for its termination i.e. '\n' or ';\n'.
I am not _aware_ of any way to check that. I'm not sure, but I think if it did exist, I could formulate a "Liar's Paradox" around it.
Hi Walter,
I am not sure that I understand the paradox, the semicolon is either there or it is not. I was wondering if I could check it just as I can check the input and output arguments. See my answer below.
With eval, I could construct a clause that had a semi-colon if and only if the test said that there was no semi-colon. Remember Matlab is interpreted and what is to be executed can be constructed on the fly.

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

추가 답변 (1개)

Jan
Jan 2011년 3월 22일
I just realized that I just want the function to output text if I use it without assigning the output argument. So it would look like this:
function a = say_hi(b)
a = b;
if (nargout == 0)
fprintf(1, 'Hello World');
end
end
The reason I did not want the 'print' argument in the list, was that maybe I want to increase the number of arguments in the future. I would not want the 'print' argument somewhere in the middle of the list, but at the end. As that would cause backwards compatibility issues, I could use an optional input argument 'DisplayOn' to solve it (rather lengthy):
function a = say_hi(b, varargin)
print = 1;
if nargout == 1
print = 0;
end
if size(varargin,2) >= 2
for j = 1:2:length(varargin)
param = varargin(j);
value = varargin(j+1);
if strcmp(param, 'DisplayOn')
if strcmpi(value, 'true')
print = 1;
else
print = 0;
end
end
end
end
a = b;
if (print == 1)
fprintf(1, 'Hello World');
end
end

댓글 수: 1

Your first function can possibly be rewritten as:
function a = say_hi(b)
if nargout
a = b;
else
fprintf(1, 'Hello World');
end
In your second function, you should use strcmpi(param, 'DisplayOn') and for the value, instead of looking for the string 'true', you should look for non-zero values, to be consistent with how parameters are defined for the Matlab libraries.
I believe there are utility routines that can do the parameter parsing for you; you can find examples in nearly any of the Mathworks-provided routines.

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

카테고리

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

제품

질문:

Jan
2011년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by