Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simulink Mask Parameter Range Checking

98 views
Skip to first unread message

Richard

unread,
Oct 21, 2009, 2:45:09 PM10/21/09
to
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

Praetorian

unread,
Oct 21, 2009, 3:55:48 PM10/21/09
to
On Oct 21, 12:45 pm, "Richard " <richard.schwa...@craneaerospace.com>
wrote:

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.

Richard

unread,
Oct 21, 2009, 4:49:06 PM10/21/09
to
Praetorian <ashish.s...@gmail.com> wrote in message <ae9f390d-f976-4af2...@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?

Franco

unread,
Oct 5, 2016, 9:40:19 PM10/5/16
to
Here there's a possible solution.

I have a mask with one "edit" object of name 'param1'. In the Callback of the mask object I write: my_mask_callback()
The function if given below.

function [] = my_mask_callback()
% Franco N. Ferrucci
% ferrucc...@gmail.com
% Oct 2016

%% Parameters
max_val = 10; % maximum allowed value of 'param1'
min_val = 0; % minimum allowed value of 'param1'
default_val = 5; % default value in case of out of bound of 'param1'

%% Read parameter 'param1'
param1 = eval(get_param(gcb,'param1'));

%% Bound cheking
if param1 < min_val || param1 > max_val
errordlg('"param1" Must be between 0 and 10. It will be changed to its default value');
param1_chk = default_val;
else
param1_chk = param1;
end

%% Update value of 'param1'
set_param(gcb,'param1',num2str(param1_chk));
0 new messages