Simulink mask using base workspace variables and mask workspace parameters in same expression
조회 수: 39 (최근 30일)
이전 댓글 표시
Hello,
I'm creating a masked block where user will enter value of ID mask parameter and based on and a data structure in base workspace the mask will set properties of some block inside subsystem.
The data structure looks like this:
Servo(1).us.min = 10;
Servo(1).us.max = 50;
Servo(2).us.min = 500;
Servo(2).us.max = 100;
It is created and stored in MATLAB base workspace.
In subsystem mask Parameters & Dialog user is choosing ID value ranging between 1 and 2.
Now the subsystem is supposed to set some block parameter value according to the picked ID, it should happen on block initialization and every time mask parameter ID is changed.
In order to achieve that, first I created two hidden mask parameters LL and UL and used them in a block of my choice and then in Initialize and ID_callback im setting their values with evalin() because i knew it could be used to access base workspace from mask. Here is the mask code:
% Initialization code section
function initialization()
UL = evalin('base','Servo(ID).us.max')
LL = evalin('base','Servo(ID).us.max')
end
% Parameter callback section
% Callback for ID
function ID_callback()
UL = evalin('base','Servo(ID).us.max')
LL = evalin('base','Servo(ID).us.max')
end
And well, all would be fine if only mask would use base workspace parameters exclusively (or only mask workspace paramters) but I want to use them both in a single expression.
Even is Servo is a base workspace parameter the ID is mask workspace paramter so if I'm using evalin() im getting error that ID is undefinied and if I'm not using evalin I'm getting error that Servo is undefinied.
Is there any way to do what I want to achieve?
댓글 수: 0
답변 (1개)
Ayush
2023년 12월 27일
Hi Multiplexer,
I understand that you want to use both base as well as the mask workspace parameters in the same expression.
It is possible if first you use “evalin” to fetch the “Servo” structure from the base workspace and then use the mask workspace variable “ID” directly within the mask initialization and callback functions. The key is to pass the ID parameter to the “evalin” function as part of the string to be evaluated. You can use “sprintf” function to construct the command string dynamically based on the ID value. Please refer to the below documentations to know more about the "sprintf” function respectively:
Here is a pseudocode for your reference:
% Initialization code section
function initialization(block)
ID = block.DialogPrm(1).Value; % Assuming 'ID' is the first dialog parameter and accessing that from the mask workspace
UL = evalin('base', sprintf('Servo(%d).us.max', ID));
LL = evalin('base', sprintf('Servo(%d).us.min', ID));
% Set the values of UL and LL in the mask workspace.
% Assuming 'UL’ and ‘LL’ as the second and third dialog parameter and accessing that from the mask workspace
block. DialogPrm(2).Value = UL;
block. DialogPrm(3).Value = LL;
end
% Parameter callback section
% Callback for ID
function ID_callback(block)
ID = block.DialogPrm(1).Data; % Assuming 'ID' is the first parameter
UL = evalin('base', sprintf('Servo(%d).us.max', ID));
LL = evalin('base', sprintf('Servo(%d).us.min', ID));
% Set the values of UL and LL in the mask workspace
% Assuming 'UL’ and ‘LL’ as the second and third dialog parameter and accessing that from the mask workspace
block. DialogPrm(2).Value = UL;
block. DialogPrm(3).Value = LL;
end
Instead of DialogPrm (used for pseudo code), you can use the “get_param” function as per the block parameters names that you have created for your mask block. Please refer to the below documentation to know more about “get_param”:
Hope it helps,
Regards,
Ayush Misra
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Author Block Masks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!