Filter löschen
Filter löschen

display max (character)

2 Ansichten (letzte 30 Tage)
amateurintraining
amateurintraining am 6 Okt. 2017
Kommentiert: Image Analyst am 7 Okt. 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 Kommentar
Cedric
Cedric am 6 Okt. 2017
Bearbeitet: Cedric am 6 Okt. 2017
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 6 Okt. 2017

Weitere Antworten (1)

Image Analyst
Image Analyst am 6 Okt. 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 Kommentare
Walter Roberson
Walter Roberson am 6 Okt. 2017
inputname()
Image Analyst
Image Analyst am 7 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by