Borrar filtros
Borrar filtros

display max (character)

2 visualizaciones (últimos 30 días)
amateurintraining
amateurintraining el 6 de Oct. de 2017
Comentada: Image Analyst el 7 de Oct. de 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 comentario
Cedric
Cedric el 6 de Oct. de 2017
Editada: Cedric el 6 de Oct. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 6 de Oct. de 2017

Más respuestas (1)

Image Analyst
Image Analyst el 6 de Oct. de 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 comentarios
Walter Roberson
Walter Roberson el 6 de Oct. de 2017
inputname()
Image Analyst
Image Analyst el 7 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by