display max (character)
5 views (last 30 days)
Show older comments
amateurintraining
on 6 Oct 2017
Commented: Image Analyst
on 7 Oct 2017
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 Comment
Accepted Answer
More Answers (1)
Image Analyst
on 6 Oct 2017
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 Comments
Image Analyst
on 7 Oct 2017
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.
See Also
Categories
Find more on Startup and Shutdown 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!