How can I generate a Command Window error from Simulink block mask editor (Initialization Tab)?

7 views (last 30 days)
I would like to do some input validation of the block mask parameters, and in the case that the input is out of range, output an error message.
Using "errordlg" is annoying because if the parameter is a workspace variable that changes and you've used the block in N places in the model, suddenly you have N pop-up dialogs.
Using the Command Window to display these errors is a more comfortable option. But when I try the following inside the Initialization tab of the mask editor:
if num<0
error('Number must be positive: "%s"', gcb);
end;
I get a pop-up anyway saying: Error due to multiple causes. --> Error in 'untitled/myblock': Initialization commands cannot be evaluated. --> Number must be positive: "untitled/myblock"
Is there any way to get Simulink to output to the Matlab command window?
Thanks!

Accepted Answer

Babak
Babak on 9 Oct 2012
disp() command would display whatever you want to display in the command window.
  3 Comments
Babak
Babak on 10 Oct 2012
I don't think or know how to do this. Please close this question with accepting the answer and create a new question with your hyperlink question such that everyone could participate answering.

Sign in to comment.

More Answers (2)

Kaustubha Govind
Kaustubha Govind on 30 Nov 2012
I think the right place to validate mask parameters is the dialog callbacks, rather than Mask Initialization. Read Dynamic Mask Dialogs for more information. This should also prevent the problem of multiple error windows popping up.
  2 Comments
Patricia
Patricia on 30 Nov 2012
Sorry I don't quite see how this could solve my problem. My main concern is validating mask parameters (Edit type) when they are workspace variables. Dialog callbacks only get executed is the mask UI is open and something has changed, but if the mask parameters change indirectly through a variable they aren't executed and my input is not validated (even after updating the model!). Please correct me if I am wrong.
Another point is that at least in my release (2011b), using "error" in the dialog callbacks still causes an even more annoying pop-up that the ones I generate myself using "errordlg" in the Mask Init.
Babak
Babak on 30 Nov 2012
error() stops execution of the code and gives a matlab error as soon as it is detected. It stops execution of matlabs mask gui fucntions which results in matlabs errors too. So for UI errors you want to spit, errordlg() should be used.
As Kaustubha mentions, if your mask user inserts variables from the mask GUI,by double clicking on the block, and you want to run some check on what the user has punched in, you should use dialog callback functions. If your mask parameters change from other places too, you may want to run a double check on them using the initialization function as well.

Sign in to comment.


Patricia
Patricia on 30 Nov 2012
Edited: Patricia on 30 Nov 2012
After my research I recommend using Block Callbacks (InitFcn) to validate inputs. Using "error" in these callbacks creates an error in the Simulink Diagnostics during update and provides a nice link and highlight to the affected block. I would only recommend this from R2012a and on. R2011b has a small bug, instead of getting 1 error you will get 2 (block and model error), but I can still live with this.

Products

Community Treasure Hunt

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

Start Hunting!