Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

Asked by Patricia on 9 Oct 2012

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);

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?





3 Answers

Answer by Babak on 9 Oct 2012
Accepted answer

disp() command would display whatever you want to display in the command window.


Patricia on 10 Oct 2012

Thanks, this would partially solve my problem. I would also like to include a hyperlink to the current block (gcb) in the message, but I haven't found a way to do this either. Any ideas?

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.

Babak on 30 Nov 2012

I think errordlg() is better to use though!

Answer by 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.


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 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.

Kaustubha Govind
Answer by Patricia on 30 Nov 2012
Edited by 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.



Contact us