display max (character)

2 次查看(过去 30 天)
Hi! I have a function and I want to display the max of two scores. For example, if A=5 and B=9 and C is the max, I want C to reply that B is the greater value. How do I do this? Thanks in advance.
  1 个评论
Cedric
Cedric 2017-10-6
编辑:Cedric 2017-10-6
Do you need any clarification about this before?
There are also many other questions for which you got an answer and didn't seem to come back and really care.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-10-6

更多回答(1 个)

Image Analyst
Image Analyst 2017-10-6
How can C, which will equal 9, reply anything? A simple number can't return anything. I assume you want the function to return the name of the biggest variable, like
A = 5;
B = 9;
varName = myFunction(A, B) % Should return a string 'B' in this case for varName.
message = sprintf('%s is the max', varName);
uiwait(helpdlg(message)));
And myFunction would be something like
function letter = myFunction(v1, v2)
if v1 > v2
etc......
And what you'd see is a popup message box with the message "B is the max". Right? I think Walter showed a way, a few months ago, where the function myFunction() could find out the name of the variable name in the calling routine but I don't remember what it was. The function was called something like invarname() or varnames() or varinputname() or something - I don't remember and can't find it now. So like that function would return "B" because it somehow knew that v2 in the function definition was really called B in the calling routine. Perhaps Walter will remind me.
  2 个评论
Image Analyst
Image Analyst 2017-10-7
Thanks Walter! Then this seems to work:
function test()
A = 5;
B = 9;
varName = myFunction(A, B) % Should return a string 'B' in this case for varName.
message = sprintf('%s is the greater value', varName);
uiwait(helpdlg(message));
end
function letter = myFunction(v1, v2)
if v1 > v2
letter = inputname(1);
else
letter = inputname(2);
end
end
It pops up a message box that says "B is the greater value" just like you asked for.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by