MATLAB Answers

display value in message box

조회 수: 318(최근 30일)
son
son 20 Aug 2014
편집: Eduardo Rey 11 Mar 2020 22:25
hi, please help.
i create a multiple choice box and then display value of a in a message box but i don't know how to put the value of 'a' in the message box.
close all
clear all
choice = menu('Choose optical fiber type','Single-mode fiber ( G.652 )','Dispersion-shifted fiber ( G.653 )','Non-zero dispersion-shifted fiber ( G.655 )');
if choice==1
a=1;
elseif choice==2
a=2;
else
a=3;
end
h = msgbox(Message)

  댓글 수: 0

로그인 to comment.

채택된 답변

Star Strider
Star Strider 20 Aug 2014
Is this:
h = msgbox(sprintf('You chose %d',a))
something like what you want to do?

  댓글 수: 2

son
son 20 Aug 2014
hi, i use sprintf code but now it can't display the special symbol alpha
clear all
close all
%%Insert value in Matlab
dlg_title = 'Insert Factor Value';
x1 = inputdlg('Enter number of channels:',dlg_title, [1 50]);
n = str2num(x1{:});
x2 = inputdlg('Enter input power for each channel (mW):',dlg_title, [1 50]);
p = str2num(x2{:})*10^-3;
x3 = inputdlg('Enter channel spacing (nm):',dlg_title, [1 50]);
delta_lambda = str2num(x3{:})*10^-9;
CreateStruct.Interpreter = 'tex';
CreateStruct.WindowStyle = 'modal';
choice = menu('Choose optical fiber type','Single-mode fiber ( G.652 )','Dispersion-shifted fiber ( G.653 )','Non-zero dispersion-shifted fiber ( G.655 )');
if choice==1
alphadb = 0.20;
dispersion=16;
slope=0.080;
A=80;
elseif choice==2
alphadb = 0.22;
dispersion=0;
slope=0.075;
A=50;
else
alphadb = 0.23;
dispersion=3;
slope=0.050;
A=72;
end
h = msgbox({sprintf('Fibre attenuation: \alpha = %g (dB/km)',alphadb)
'Derivative dispersion coefficient: s = dD/d\lambda = 0.075 (ps/nm^2.km)' },'Standard Values',CreateStruct);
Star Strider
Star Strider 20 Aug 2014
In sprintf, single ‘\’ are control characters for it and will throw an error if sprintf doesn’t recognise a valid control sequence, so to get it to print a ‘\’ that the TeX interpreter will see and interpret requires a double ‘\\’.
This worked for me, displaying both alpha and lambda as special characters:
h = msgbox({sprintf('Fibre attenuation: \\alpha = %g (dB/km)',alphadb)
'Derivative dispersion coefficient: s = dD/d\lambda = 0.075 (ps/nm^2.km)' },'Standard Values',CreateStruct);

로그인 to comment.

추가 답변(3개)

Image Analyst
Image Analyst 20 Aug 2014
son, you don't need to get the handle from the message box if you don't ever use it. And you might want to use uiwait() and make it modal, rather than the way you and the others did it, so that it waits for the user to click OK before blasting onwards, executing subsequent code before the users have even had a chance to click OK yet.
message = sprintf('You clicked on button #%d,\nwhich set a = %f', choice, a);
uiwait(msgbox(message, 'modal'));
Alternatively you can use helpdlg() instead of msgbox() if you want an "text balloon" displayed or warndlg() if you want an exclamation point displayed.
uiwait(helpdlg(message)); % Has text balloon
uiwait(warndlg(message)); % Has exclamation symbol
Hope this helps. Vote for my answer if it was helpful additional information.

  댓글 수: 0

로그인 to comment.


Ben11
Ben11 20 Aug 2014
You can use sprintf to format the message:
Message = sprintf('The value selected is %d\n',a);
h = msgbox(Message)

  댓글 수: 0

로그인 to comment.


son
son 20 Aug 2014
woa, so easy, many thanks

  댓글 수: 2

Star Strider
Star Strider 20 Aug 2014
My pleasure!
Eduardo Rey
Eduardo Rey 11 Mar 2020 22:23
How do I display a list of values in a message box? For example, I have 10 variables:
var1 = 10e-3
var2 = 12e-3
...and so on. Basically what I want it to look like is:
var1 name: 10e-3
var2 name: 12e-3

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by