error messeges in MATLAB app designer

I have an elaborate MATLAB code and i am now working on a user interface in matlab app designer.
the code contains a long list of error messeges,warnings, and notiications displayed in the command prompt. i would like for the GUI to display these messeges. is there a way to do this without touching the code itself? i mean somehow change the GUI so that any error messeges or warnings are automatically displayed in some form? or simply to display the command prompt as part of my app?
I saw this answer:
but it seems to me a bit of an ugly solution. Is there a better one?
Many thanks
Nathan

 Réponse acceptée

Rik
Rik le 25 Jan 2021
Modifié(e) : Rik le 25 Jan 2021

1 vote

Personally I have started using the attached functions (e.g. in my readfile function). With only minimal changes in the syntax of my functions I can catch errors and warnings and redirect them to a log file or to a String property (e.g. a status window). I plan to integrate this in a GUI, similar to what your goal seems to be.
When it comes to errors, these functions assume that an actual error is required (e.g. to trigger a catch-block). Feel free to adapt them to your needs.

9 commentaires

Nathan Blanc
Nathan Blanc le 26 Jan 2021
thank you Rik! I will look into it
Rik
Rik le 30 Jan 2021
Follow-up comment sent by email:
Hello Rik, I tried to use your functions "error_" and "warning_" and failed. it seems that I am doing something wrong with the input paremeters. are there an instructions available, more elaborate than the ones in the comments of the function itself?
Rik
Rik le 30 Jan 2021
I'm afraid I don't have any additional things to say. What was your exact usage? It should all be fairly straightforward. The first input is the struct that controls the outputs, the other inputs should be the same as your original call to warning or error. So what code are you using and how does the result differ from what you want?
Nathan Blanc
Nathan Blanc le 30 Jan 2021
Thanks for your answer Rik. I assume this is a simple syntax misunderstanding issue. I tried:
options.obj=true
error_(options,'write me an error')
and obtained:
Dot indexing is not supported for variables of this type.
Error in error_ (line 71)
if options.obj.boolean
afterwards i tried
options.obj.boolean=true
error_(options,'write me an error')
and obtained :
Reference to non-existent field 'obj'.
Error in error_ (line 78)
set(options.obj.obj,'String',msg_)
Nathan
Rik
Rik le 30 Jan 2021
You need to specify the actual object. If you don't specify the object, how can Matlab know which String should display the message?
I'll probably change this code so it sets the boolean flag automatically.
Nathan Blanc
Nathan Blanc le 30 Jan 2021
Sorry for being difficult, I still don't understand. what do you mean by specifying the actual object? could you give an example?
Nathan
Like this:
plot(rand(2))
h=text(1.5,0.5,'foo');
axis([1 2 0 1])
options=struct;
options.obj.obj=h;
options.obj.boolean=true;
error_(options,'write me an error')
write me an error

Error in solution (line 9)

Error in evaluateRegions

Error in SynchronousEvaluationOutputsService/evalRegions

Error in SynchronousEvaluationOutputsService/evaluateSynchronously

Error in evaluateSynchronousRequest

Error in fevalMatlab

Error in fevalJSON
Nathan Blanc
Nathan Blanc le 31 Jan 2021
I see
many thanks
Rik
Rik le 31 Jan 2021
You're welcome.

Connectez-vous pour commenter.

Plus de réponses (1)

Adam Danz
Adam Danz le 25 Jan 2021
Modifié(e) : Adam Danz le 25 Jan 2021

1 vote

There isn't a pretty solution and there is no solution that doesn't involve touching the code itself. I've often wished there were a method to embed the command window into an app window but there isn't.
Since it doesn't sound like you want to implement a non-pretty solution that involves touching the code I won't go into more detail unless you want it but it involves try/catch blocks that will catch errors which you can send to a GUI text window. Warnings are trickier. Catching them would require undocumented methods.
What's the problem with the command window? If you're worried that users may not pay attention to it, there's a way to put the command window in their face when an error/warning is detected. It requries changes to the code, of course, so I'll describe it briefly, assuming you don't want to do that work.
The commandwindow() command opens and displays the command window even if an app is on top of it. You can use try/catch statements to detect the error and commandwindow within the catch-block to make sure the user sees the errors. Alternatively, you could wrap the error in an error dialog that displays on top of the app. For warnings, you can use lastwarn at the end of each function to detect if a warning was thrown and if so, use commandwindow to bring the command window into focus or wrap the printed warning in a dialog that appears on top of the app.

2 commentaires

Nathan Blanc
Nathan Blanc le 25 Jan 2021
Thanks for your answer Adam
The issue is I eventually want to transform this code into a standalone app, so I can't count on the command window itself. I guess my best solution would ineed be to change the code itself
Nathan
Adam Danz
Adam Danz le 25 Jan 2021
If the warnings are created by the code, use uialert instead.
If the warnings are Matlab warnings you might be able to address the warnings to prevent them; you can always turn them off if they are to be ignored.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Code Execution dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by