command to stop program if there is no enough input parameters?

3 ビュー (過去 30 日間)
Nikola
Nikola 2015 年 3 月 12 日
回答済み: Nikola 2015 年 3 月 12 日
I made some function file that plot x versus y with extrema max min etc... I need to find command that will stop program if there is not enough input parameters. First input paremeter is "x" and second "y", and there is few more input parameters but they arent important now.
if nargin <2 || isempty(x0)
disp('Must enter two input parameters for plot.')
end
Program will display message, but what I have to do to make program stop if there arent at least 2 input parameters?

採用された回答

Julia
Julia 2015 年 3 月 12 日
Hi,
include the return command to stop your program.

その他の回答 (2 件)

Guillaume
Guillaume 2015 年 3 月 12 日
The normal way to stop a program when a precondition is not met is to issue an error. Not only does it stop the program, it also displays the message in red.
if nargin < 2 || isempty(x0)
error('Must enter two input parameters for plot.');
end
Note that there are a number of functions in matlab that can help you validate your preconditions and throw an error when these are not met, for example validateattributes, validatestring, narginchk and nargoutchk. In your particular case
narginchk(2, 2);
validateattributes(x0, {'numeric'}, {'nonempty'});

Nikola
Nikola 2015 年 3 月 12 日
OK, error command do what I need. Thanks

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by