Thread Subject: Simulink Mask Parameter Range Checking

Subject: Simulink Mask Parameter Range Checking

From: Richard

Date: 21 Oct, 2009 18:45:09

Message: 1 of 3

I am new to dealing with callback functions, and had a question about range checking, and throwing an error.

I have a masked subsystem with two parameters that I would like to range check. I want to ensure that the value entered is a positive integer. I currently have the following initialization command callback function that runs whenever the block is initialized. Both parameters have the Evaluate box checked in the Dialog parameters field of the Mask Editor, which as far as I can tell returns null if a string that is not a valid variable name is entered instead of a number or variable. Is this true?

function range_check(varName, errMsgName)
    %check that varName is not empty (meaning a string was entered),
    %is an integer, and is positive
    if (isempty(varName) || ...
        rem(varName, 1) ~= 0 || ...
       (rem(varName, 1) == 0 && varName < 1))
        errordlg(['Number of ' errMsgName ' must be a ' ...
                 'positive integer.'], ...
                 'Error: Function Block Parameters');
    end %if
end %function

This gives me an error if I click OK or APPLY (as desired), but the value still accepted, and if I click OK after the error has displayed, the Function Block Parameter box disappears, and the invalid parameter value is stored. What is the best way to display an error message and not store the invalid value?

Thanks,

Richard

Subject: Simulink Mask Parameter Range Checking

From: Praetorian

Date: 21 Oct, 2009 19:55:48

Message: 2 of 3

On Oct 21, 12:45 pm, "Richard " <richard.schwa...@craneaerospace.com>
wrote:
> I am new to dealing with callback functions, and had a question about range checking, and throwing an error.
>
> I have a masked subsystem with two parameters that I would like to range check.  I want to ensure that the value entered is a positive integer.  I currently have the following initialization command callback function that runs whenever the block is initialized.  Both parameters have the Evaluate box checked in the Dialog parameters field of the Mask Editor, which as far as I can tell returns null if a string that is not a valid variable name is entered instead of a number or variable.  Is this true?
>
> function range_check(varName, errMsgName)      
>     %check that varName is not empty (meaning a string was entered),
>     %is an integer, and is positive
>     if (isempty(varName) || ...
>         rem(varName, 1) ~= 0 || ...
>        (rem(varName, 1) == 0 && varName < 1))
>         errordlg(['Number of ' errMsgName ' must be a ' ...
>                  'positive integer.'], ...
>                  'Error: Function Block Parameters');
>     end %if
> end %function
>
> This gives me an error if I click OK or APPLY (as desired), but the value still accepted, and if I click OK after the error has displayed, the Function Block Parameter box disappears, and the invalid parameter value is stored.  What is the best way to display an error message and not store the invalid value?
>
> Thanks,
>
> Richard

The way I've dealt with this in the past is to have a hidden edit box
which holds the last valid entry. If the current entry is invalid then
you load the contents of this hidden edit box into the visible one and
display the error, all within your callback function. Maybe
(hopefully) someone has a better solution.

HTH,
Ashish.

Subject: Simulink Mask Parameter Range Checking

From: Richard

Date: 21 Oct, 2009 20:49:06

Message: 3 of 3

Praetorian <ashish.sadanandan@gmail.com> wrote in message <ae9f390d-f976-4af2-b8f8-64ea7d7b485b@d34g2000vbm.googlegroups.com>...
> On Oct 21, 12:45?pm, "Richard " <richard.schwa...@craneaerospace.com>
> wrote:
> > I am new to dealing with callback functions, and had a question about range checking, and throwing an error.
> >
> > I have a masked subsystem with two parameters that I would like to range check. ?I want to ensure that the value entered is a positive integer. ?I currently have the following initialization command callback function that runs whenever the block is initialized. ?Both parameters have the Evaluate box checked in the Dialog parameters field of the Mask Editor, which as far as I can tell returns null if a string that is not a valid variable name is entered instead of a number or variable. ?Is this true?
> >
> > function range_check(varName, errMsgName) ? ? ?
> > ? ? %check that varName is not empty (meaning a string was entered),
> > ? ? %is an integer, and is positive
> > ? ? if (isempty(varName) || ...
> > ? ? ? ? rem(varName, 1) ~= 0 || ...
> > ? ? ? ?(rem(varName, 1) == 0 && varName < 1))
> > ? ? ? ? errordlg(['Number of ' errMsgName ' must be a ' ...
> > ? ? ? ? ? ? ? ? ?'positive integer.'], ...
> > ? ? ? ? ? ? ? ? ?'Error: Function Block Parameters');
> > ? ? end %if
> > end %function
> >
> > This gives me an error if I click OK or APPLY (as desired), but the value still accepted, and if I click OK after the error has displayed, the Function Block Parameter box disappears, and the invalid parameter value is stored. ?What is the best way to display an error message and not store the invalid value?
> >
> > Thanks,
> >
> > Richard
>
> The way I've dealt with this in the past is to have a hidden edit box
> which holds the last valid entry. If the current entry is invalid then
> you load the contents of this hidden edit box into the visible one and
> display the error, all within your callback function. Maybe
> (hopefully) someone has a better solution.
>
> HTH,
> Ashish.

This could work, however I was thinking more along the lines of how Simulink does it with their blocks. For instance if you have a Logical Operator block and put -2 as the number of input ports or an AND operator, the field turns tan showing that the value has been changed, and I think this means that the Initialization callback has to be executed. If you click Apply, an error is generated, the field turns white, but then when you click OK on the error message, the incorrect -2 stays in the field, but the field is tan again, and Apply is still active, so if you were to click OK or Apply, it would re-generate the error. Is there a parameter that i can use with set_param to do something like field_changed = true and thus re-run my callback range check function if OK or Apply is clicked again?

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
simulink Richard 21 Oct, 2009 14:49:07
masked subsystems Richard 21 Oct, 2009 14:49:07
callback functions Richard 21 Oct, 2009 14:49:07
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com