MATLAB End a Program in MATLAB with Error-Checking: Troubleshooting Tips

  • Thread starter Thread starter Jelfish
  • Start date Start date
  • Tags Tags
    Matlab Program
Click For Summary
In a MATLAB programming discussion, a user sought help on how to terminate a program when a specific error condition is met, specifically when the sizes of a matrix and vector do not match. The solution provided involved using the 'error' function to display a custom error message and halt the program execution. The example code included an if-statement that checks the condition and calls 'fprintf' to print an error message before invoking 'error' to stop the program. Additional suggestions included using the 'return' command if working within a function context. The user expressed gratitude for the assistance received.
Jelfish
Messages
147
Reaction score
5
Hi

I've been trying to figure this out for a bit and thought one of you might know the answer off the top of your head.

I have an if-statement in my MATLAB program that error checks a matrix and vector size. I want the program to print an error message and then end the program (like a ctrl-c) if the error condition is satisfied. Something like this:

Code:
if a~=b
fprintf('STFU N00B');
??
end
% other stuff

where ? is the function the would end the program. Does such a function exist, and if so, what is it?

Thanks in advance.
 
Last edited:
Physics news on Phys.org
STFU NOOB? Heh heh heh...That's funny. Sorry I can't help on the Matlab part.
 
Jelfish said:
Hi
I've been trying to figure this out for a bit and thought one of you might know the answer off the top of your head.
I have an if-statement in my MATLAB program that error checks a matrix and vector size. I want the program to print an error message and then end the program (like a ctrl-c) if the error condition is satisfied. Something like this:
Code:
if a~=b
fprintf('STFU N00B');
??
end
% other stuff
where ? is the function the would end the program. Does such a function exist, and if so, what is it?
Thanks in advance.
Code:
if a~=b
fprintf('STFU N00B');
error('u R not 1337')
end
% other stuff
 
if you are writing a function, your output variables have been defined, and you want them passed, you can also use the 'return' command.
 
yes there is a breaker that ends the MATLAB exe...unfortunately i haven't played with MATLAB for 3 years.
 
LeBrad said:
Code:
if a~=b
fprintf('STFU N00B');
error('u R not 1337')
end
% other stuff

Thanks, Brad! That's exactly what I needed. LeBrad = awesome;

And thanks to everyone else too
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 14 ·
Replies
14
Views
3K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K