MATLAB Answers


functions in matlab gui

Asked by Huseyin on 21 Mar 2014
Latest activity Commented on by Salaheddin Hosseinzadeh on 21 Mar 2014

Hi, I have created a function to use in GUI. The problem is that the function sometimes doesn't response to specific inputs and it gives an error on command window. This is normal and there is no problem until here. On GUI, I want to create a dialog box for this situation like 'Wrong Input'. I know the code for the dialog box, but I could'nt figure out how to add it. I think it should be like if that function doesnt work, answer will be zero and if it works the answer will be one. If I can create something like that it will be possible to add that dialog box when the function doesn't work. How can I do that?

Simply, how can I see the response of the function? (like 1 or 0) Thanks


Your question is vague to me! I think U don't know how to pass variables from one GUI to the other one, is it so?! Unfortunately, I don't know it or at least can't remember! Do a favor, and clarify your question cuz I don't get anything!

Ok let me explain it more clearly and simply. When the function works the output of the function is a matrix with the size [n,m] . When it didn't work, it gives me an error message on command window like 'Attempted to access mywork(0); index must be a positive integer or logical.' What I want to do is that if the function did not work properly, I want to see a dialog box (no error message on command window)

Log in to comment.

3 Answers

Answer by Jan Simon
on 21 Mar 2014
 Accepted Answer

It depends on what "the function does not respond" exactly means. If it stops with an error message, use try catch to catch the error and the corresponding message. If a specific output is replied, simply obtain this output, when you call the function from the callback of the GUI. Afterwards you can display the results how ever you want.

If is exactly the same situation, when you call a built-in function like e.g. sin(). When the input is a string, an error is thrown. When invalid input values are provided, e.g. NaN, NaN is replied also, such that you can catch this by checking the output.

Finally I assume, the actual question is not clear yet.


Log in to comment.

Answer by Salaheddin Hosseinzadeh on 21 Mar 2014

@ Husaiyn

Alright now it's clear!

Go to the error corresponding line, error line is probably given to you in the MATLAB error, or you may already know where is the error.

put it in a try catch

   more code....
  diagbox('corresponding error');

and you won't see any error on the command window, instead you will see a dialogbox with a message and ...

I'm not sure if diagbox is the correct speling, check the MATLAB help plz, also for further options about dialog box.

Good Luck!

  1 Comment

Yes! It worked, thanks a lot!

Log in to comment.

Answer by Salaheddin Hosseinzadeh on 21 Mar 2014

Actually, I think diagbox is for VC, I'm sure you already found it. but You may use this code

msgbox('Error Message','Name the msgbox','warn')


Actually I use the same code as you said:) But thanks anyway


I'm sorry I'm still confused where I used diagbox or something very similar to diagbox, dialogbox or ...

It was either VC#, VC++, MATLAB! Or maybe none! I don't know.

Glad that your prob is solved ;)

Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today