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

Simulink: Prevent block resizing/moving (callback recursions)

140 views
Skip to first unread message

Janos Kutscherauer

unread,
Jul 8, 2010, 8:01:04 AM7/8/10
to
Hi,

i am trying to facilitate a mechanism to lock blocks in Simulink, so that users cannot move or resize them. My approach is to catch the "MoveFcn" callback (which is triggered when a block was moved or resized), and set the blocks position to the previously stored value.

Simulink recognizes, that setting the 'Position' parameter within the 'MoveFcn' callback handler might cause a recursion, and although it actually executes my code, it throws the warning:

Warning: Error evaluating 'MoveFcn' callback of Scope block 'simpleModel/Scope'. Error using ==> set_param Recursion detected in the MoveFcn of 'simpleModel/Scope'.

I thought i could workaround it by first unsetting the 'MoveFcn' callback function, then resetting the position value, and finally setting the callback function again. But still Simulink recognized recursion.

This is the code, which even checks if the new value is different to the stored value.

> %checkLock.m
> set_param(gcb, 'MoveFcn', '');
> storedPos = [470 64 500 96];
> currentPos = get_param(gcb, 'Position');
> if ~isequal(storedPos, currentPos)
> set_param(gcb, 'Position', storedPos);
> end
> set_param(gcb, 'MoveFcn', checkLock);

This produces the warning mentioned above. I think the code takes care of recursions pretty well (not sure about the timing, when i unregister and re-register the callback function, but the condition does prevent recursive calls!).

Is there any workaround specifically for preventing resizing/movement of blocks, or more generally for handling this kind of recursion warnings?

Thanks for your comments!
Cheers
Janos

John Stevens

unread,
Oct 4, 2010, 4:25:26 PM10/4/10
to
The trick is to use the try statement to prevent the recursion error from tripping and terminating the callback. In my example below, a flag called 'InMoveFcn' is defined withing the User Data, which is set to persistent. This code allows the block to be moved, but not resized.

%-- start code

user_data = get_param(gcb,'UserData');

if user_data.InMoveFcn == 0

user_data.InMoveFcn = 1;
set_param(gcb,'UserData',user_data);

blk_pos = get_param(gcb,'Position');
[img_height,img_width,tmp] = size(user_data.im);

try
set_param(gcb,'Position',[blk_pos(1) blk_pos(2) blk_pos(1)+img_width blk_pos(2)+img_height]);
catch ME
end

user_data.InMoveFcn = 0;
set_param(gcb,'UserData',user_data);

clear user_data img_height img_width tmp;

end

%-- end code

0 new messages