Creating my own error within a program

8 Ansichten (letzte 30 Tage)
Zachary
Zachary am 14 Jun. 2011
I just have a quick question... So I'm writing a program that adapts to whatever variable names and what values they hold. I wanted to add a bit that checks the user entered variables quickly using isvarname to ensure that they were all viable names before too much time and memory was wasted doing other things. This I have down. What I need clarification on, and possibly a step-by-step guide to, is writing a custom error message to display and trigger a catch block.
Do I just need to write out the error( '...' , '...' ) bit and call it good or do I need additional code there for matlab to actually catch the error?
Thanks in advance for the help,
Zach

Antworten (1)

Matt Fig
Matt Fig am 14 Jun. 2011
Is your program a function? If so, then it doesn't matter what variable names the user passes, because your function will have its own variable names. For example, consider this function:
function b = myfun(a)
b = a;
Now when the user calls this with say,
myfun(var_name)
Your function will never see var_name, only a.
If you're using a script, then MATLAB won't let the user use a bad varname anyway. For example, try to make this variable:
1f = 5; % Errors.
For help with using the ERROR function, see the help!
help error
help try
  2 Kommentare
Zachary
Zachary am 14 Jun. 2011
I am writing a function, however the way I wrote it I didn't give it any input, so it looks like b = myfun()
I did that because I have no way of anticipating how many variables the user will pass into the function. So instead of the user calling it with a variable already in place, they simply call it and using the input command, I collect all the information I need to run the calculation the program is being written for.
See it bugs the heck out of me that a lot of in-built commands in matlab REQUIRE the input to be formatted in a particular way, because often you spend more time trying to figure out how to get it the way they want than it takes to just suck it up and do the problem by hand. So the program I'm writing adapts to how the user chooses to input the data, to a point.
I've read through the help files and they are unclear if just putting the error part in there is enough or if I need to make an MException thingy too.
Matt Fig
Matt Fig am 14 Jun. 2011
function b = myfun(varargin)
% Describe in detail how many arguments can be accepted,
% and the order of these arguments. In this example, say
% the user can input up to two values.
N = nargin; % Tell how many inputs the user provided.
if N==0;
a = 1; % Default values of two inputs a and c
c = 4;
elseif N==1
a = varargin{1};
c = 4;
elseif N==2
a = varargin{1};
c = varargin{2};
else
error('Too many inputs')
end
% Rest of the code using a and c....

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Scope Variables and Generate Names finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by