quit program using if

4 次查看(过去 30 天)
son
son 2014-8-30
hi, i create a program:
% optimal algorithm
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;
x4 = inputdlg('Enter distance transmission (km):',dlg_title, [1 50]);
distance = str2num(x4{:});
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;
recommend=sprintf('Single-mode fiber ( G.652 ) is not suitable for this situation');
elseif choice==2
alphadb = 0.22;
dispersion=0.001;
slope=0.075;
A=50;
recommend=sprintf('Dispersion-shifted fiber ( G.653 ) is not suitable for this situation');
else
alphadb = 0.23;
dispersion=3;
slope=0.050;
A=72;
recommend=sprintf('Non-zero dispersion-shifted fiber ( G.655 ) is not suitable for this situation');
end
B=2.5
i also have some code below
now if the: distance > 104000/(B^2*dispersion) , i want to DISPLAY a message box: "distance is not good. try again" and QUIT the program, else the program CONTINUES .

采纳的回答

Image Analyst
Image Analyst 2014-8-30
Try this:
maxAllowedValue = 104000/(B^2*dispersion) ;
if distance > maxAllowedValue
message = sprintf('Distance of %s is greater than the max allowed value of %f.\nPlease try again.', distance, maxAllowedValue);
uiwait(warndlg(message));
return; % Would be nicer to user to use a while loop rather than exit the program.
end
  2 个评论
son
son 2014-8-30
i have use:'return' but the program doesn;t start again?
Image Analyst
Image Analyst 2014-8-30
That's right. Because you asked that it " QUIT the program", so I did exactly what you asked for. I presume you were going to have your use manually restart the program, that's why my comment said it would be nicer to the user to have that part in a while loop instead of calling return to quit the program.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by