- 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.
I can´t break the main loop with my code on function file
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Madalena Francisco
el 22 de En. de 2023
Comentada: Madalena Francisco
el 23 de En. de 2023
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
0 comentarios
Respuesta aceptada
Walter Roberson
el 23 de En. de 2023
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:
3 comentarios
Walter Roberson
el 23 de En. de 2023
if b==1 %Menu operation b 'Through a file'
clc
tp = a_file ();
if tp == 1; break; end
with
function tp = a_file ()
Más respuestas (0)
Ver también
Categorías
Más información sobre Web Services en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!