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

조회 수: 6 (최근 30일)
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!

채택된 답변

Babak
Babak 2012년 10월 9일
disp() command would display whatever you want to display in the command window.
  댓글 수: 3
Babak
Babak 2012년 10월 10일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Kaustubha Govind
Kaustubha Govind 2012년 11월 30일
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
Patricia
Patricia 2012년 11월 30일
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 2012년 11월 30일
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.

댓글을 달려면 로그인하십시오.


Patricia
Patricia 2012년 11월 30일
편집: Patricia 2012년 11월 30일
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.

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by