How can i display variable name on message box?

The case is:
in workspace, there are some variable name and corresponding value
for example
name=value
A=1
B=2
C=3
D=4
How to display the minimum value included variable name in message box: A=1

 Réponse acceptée

Try this:
A=1;
B=2;
C=3;
D=4;
varcell = {'A','B','C','D'};
varvctr = [A B C D];
[minvar,idx] = min(varvctr);
msg = sprintf('%s = %d',varcell{idx},varvctr(idx));
msgbox(msg, 'Minimum: ')

8 commentaires

yt man
yt man le 18 Fév 2017
Thank you so much, its work!
Star Strider
Star Strider le 18 Fév 2017
My pleasure!
If my Answer solved your problem, please Accept it!
yt man
yt man le 18 Fév 2017
Modifié(e) : yt man le 18 Fév 2017
i try another method to show the min ans,by table
A=40;
B=6;
C=10;
D=21;
E=17;
F=22;
Name = {'A';'B';'C';'D';'E';'F'};
Value = [A;B;C;D;E;F];
T = table(Name,Value)
rows = min(Value);
vars = {'Name'};
Final = T(rows,vars)
why the final table will not show the name of minimum value, it show the F or 'Row index exceeds table dimensions'.
Notice that Star Strider used
[minvar,idx] = min(varvctr);
and then used the idx value.
You have used
rows = min(Value);
which returns the minimum value but not its position.
I still not understand how can i show it,
Name
____
'B'
if i change
rows = min(Value);
to
[minvar,idx] = min(Value);
it still cannot show what i want :(
yt man
yt man le 18 Fév 2017
Thanks Walter, its work!!!
Star Strider
Star Strider le 18 Fév 2017
Thank you Walter!
Fast asleep here (UTC-7) during that exchange.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by