如果用户输入错误的输入,如何退出 matlab m 文件(不是 matlab 本身)?

发布于 2024-09-13 05:58:52 字数 171 浏览 2 评论 0原文

如果用户输入错误的输入,如何退出 matlab m 文件(不是 matlab 本身)? 我知道如果 m 文件在运行时出错,我们可以按 Ctrl-C 来停止它。但我需要一个命令将其放入我的 m 文件中,以便在出现问题时执行此操作。

请不要建议“退出”或“退出”命令,因为它们会终止整个 matlab,而我不想要它。

How to exit a matlab m-file (NOT the matlab itself) if the user enters bad inputs?
I know if a m-file goes wrong at run time we can press Ctrl-C to stop it. but I need a command to put it in my m-file to do so if something bad happens.

Please don't suggest 'exit' or 'quit' commands as they terminate the entire matlab and I don't want it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦亦如幻 2024-09-20 05:58:52

我不确定你如何定义“退出”,但 error 似乎是你需要的功能。

y = input('Please input a non-negative number: ');
if(y<0)
    error('input must be non-negative');
end

disp( sprintf('y=%f', y ) );

I am not sure how you define "exit", but error seems to be the function you need.

y = input('Please input a non-negative number: ');
if(y<0)
    error('input must be non-negative');
end

disp( sprintf('y=%f', y ) );
长伴 2024-09-20 05:58:52

嘿,我想你可以使用 try-catch 组合来处理一些意外的错误并采取一些措施。

举个例子,

function [ output ] = test(input)

  Bmat = [ 1 1 1 ]   % Some matrix

  try
    input*B;
  catch ME
    disp(ME.message)
    return;          % This is the statement that exits your function
  end

end

如果你运行

>> test([1 1 1])

它,它将不起作用,因为变量“input”和“B”的内部尺寸不匹配,但“try”语句将引发“catch”异常,并从那里做你想做的任何事情。在这种情况下,它将在命令行显示错误消息并退出该函数。

这里的变量“ME”只是一个用于错误处理的 MATLAB 对象,ME.message 存储一个包含解释器捕获的错误类型的字符串。

我刚刚再次阅读您的问题...我认为命令“return”可能是您真正想要的,您将能够使用它从任何逻辑或循环语句以及函数中退出。

您可以从 MATLAB 文档中了解有关“return”命令和错误处理的更多信息,

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/return.html

Hey I suppose you could use a try-catch combination to handle a somewhat unexpected error and do something about it.

As an example,

function [ output ] = test(input)

  Bmat = [ 1 1 1 ]   % Some matrix

  try
    input*B;
  catch ME
    disp(ME.message)
    return;          % This is the statement that exits your function
  end

end

If you run

>> test([1 1 1])

It won't work since the variables 'input' and 'B' have mismatched inner dimensions, but the 'try' statement will throw an exception to 'catch', and do whatever you want from there. In this case, it will display an error message at the command line and exit the function.

The variable 'ME' here is just a MATLAB object for error handling, and ME.message stores a string containing the type of error the interpreter caught.

I just read your question again... I assume the command 'return' is probably what you are really after, you will be able use it to exit from any logic or loop statements, as well as functions.

You can read more about the 'return' command and error handling from the MATLAB documentation,

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/return.html

仙气飘飘 2024-09-20 05:58:52

您可以只输入一个错误命令,例如 error('bad user input') ,它应该会停止脚本。

编辑:或者,您可以重构代码,使其不运行,除非您将输入标志设置为 true。像这样的东西

inp = input('>', s)

if validateInput(inp)
    %do you stuff here or call your main function
else
    fprintf('Invalid input')
end

You can just put a error command like error('bad user input') and it should stop the script.

Edit: alternatively, you could just refactor your code to not run unless you set the input flag to be true. Something like

inp = input('>', s)

if validateInput(inp)
    %do you stuff here or call your main function
else
    fprintf('Invalid input')
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文