フィルターのクリア

I can´t break the main loop with my code on function file

6 ビュー (過去 30 日間)
Madalena Francisco
Madalena Francisco 2023 年 1 月 22 日
コメント済み: Madalena Francisco 2023 年 1 月 23 日
Hi my program it´s working just fine, but I have little problem... I got a 'main while loop' on my script file, and then i create a function file, and when the function file is executed i can´t break the main loop, i try to use if - break, inside the function file, and it´s not possible, i try to use if - break after i call my function, but breaks somewhere on my code when the function file is executed, so I really don´t know how I will break the main loop with my code on function file.
I create a function files to have less lines of code btw.
%script file
%.....
while true %While Main Loop
if a==1
clc
b= menu('How do you want to insert the experimental points?', ... %Menu presentation b
'Through a file','Manually');
end
if b==1 %Menu operation b 'Through a file'
clc
a_file ()
%.........
%and if i use here break command the program don´t execute as i expect
%function file
function a_file ()
disp('Enter below the name of the Excel file that contains the values you want to download.')
disp('ATTENTION: Do not forget to put the end of the file.')
fi= input('Enter the file name: ','s');
while ~exist(fi,'file')
click
warndlg({'The file name was not found.','Please check that you are in the correct directory.'})
disp('Indicate the name of the excel file that contains the values you want to download.')
disp('Warning: Don't forget to put the end of the file.')
fi= input('Enter the file name: ','s');
end
%........... code
tp= str2double(input('Enter your option: ','s')); %User option regarding menu d
while ((tp~=1) && (tp~=2)) || (isempty(tp) || isnan(tp))
errordlg ({'Incorrect option entry.','Please click Ok and enter the desired option in the Command Window.'})
tp= str2double(input('Enter your option: ','s'));
end
if tp==2
clc
msgbox('The program will continue.')
end
end
end
if tp==1
clc
%i can't use break here
msgbox('End of program.')
end
% .......
%I'm trying to break the main loop that´s on script file when my tp variable it´s equal to 1

採用された回答

Walter Roberson
Walter Roberson 2023 年 1 月 23 日
A break statement can only occur inside a for or while loop. When it is encountered, it causes the termination only of the innermost enclosing loop.
You could potentially have a loop inside of a_file but break inside that file would terminate only that loop and not the function a_file itself.
To have function a_file terminate a loop in its calling function or script, there are two possibilities:
  • function a_file could use error to force abornal termination of function a_file . Calling functions up the call chain would also be terminated, until you either got back to the command line or else got to a level that was executing inside a try, catch block. This is technically valid programming, but unless the condition represents some actual error or unhandled condition, it is often not considered good programming style. (I have heard, though, that there is a school of programming that says that you should use throwing errors and catching errors routinely; the claim is something along the lines that throwing an error allows you to construct more compact code than nested if/else chains to handle the combinations of errors that can occur.)
  • you can have a_file return a value that indicates whether it is asking the caller to terminate the current loop; if so then the calling function would break or as appropriate to end its section. This is generally considered better program structure.
  3 件のコメント
Walter Roberson
Walter Roberson 2023 年 1 月 23 日
if b==1 %Menu operation b 'Through a file'
clc
tp = a_file ();
if tp == 1; break; end
with
function tp = a_file ()
Madalena Francisco
Madalena Francisco 2023 年 1 月 23 日
It really works wow!!! Thank u so so much, words are enough to thank u!
So it is important to define as a output tp? If i don´t define the break of main while loop will not happening right?
Wooow

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by