quit program using if

조회 수: 1 (최근 30일)
son
son 2014년 8월 30일
댓글: Image Analyst 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