quit program using if

1 view (last 30 days)
son
son on 30 Aug 2014
Commented: Image Analyst on 30 Aug 2014
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 .

Accepted Answer

Image Analyst
Image Analyst on 30 Aug 2014
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 Comments
son
son on 30 Aug 2014
i have use:'return' but the program doesn;t start again?
Image Analyst
Image Analyst on 30 Aug 2014
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!