如果用户输入错误的输入,如何退出 matlab m 文件(不是 matlab 本身)?
如果用户输入错误的输入,如何退出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定你如何定义“退出”,但
error
似乎是你需要的功能。I am not sure how you define "exit", but
error
seems to be the function you need.嘿,我想你可以使用 try-catch 组合来处理一些意外的错误并采取一些措施。
举个例子,
如果你运行
它,它将不起作用,因为变量“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,
If you run
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
您可以只输入一个错误命令,例如 error('bad user input') ,它应该会停止脚本。
编辑:或者,您可以重构代码,使其不运行,除非您将输入标志设置为 true。像这样的东西
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