Azzera filtri
Azzera filtri

display max (character)

2 visualizzazioni (ultimi 30 giorni)
amateurintraining
amateurintraining il 6 Ott 2017
Commentato: Image Analyst il 7 Ott 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 Commento
Cedric
Cedric il 6 Ott 2017
Modificato: Cedric il 6 Ott 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.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 6 Ott 2017

Più risposte (1)

Image Analyst
Image Analyst il 6 Ott 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 Commenti
Walter Roberson
Walter Roberson il 6 Ott 2017
inputname()
Image Analyst
Image Analyst il 7 Ott 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.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by