如何在 simulink 中为块创建自己的参数或属性?
How can I create my own parameters or attributes for a block in simulink?
在这种情况下,我试图创建一个新的块参数作为新 属性 来保存我不想保存在已经为其他人保留的默认参数中的特定新数据不同的相关数据
对于这个参数我想使用命令get_param和set_para存在
我的意思是使用默认参数,那些。
https://edoras.sdsu.edu/doc/matlab/toolbox/simulink/slref/parameters2.html#7515
许多工具箱中的 Simulink 块是使用 MATLAB 系统对象创建的。如果要为现有 Simulink 块创建新参数,您可能需要在附带的 System object 代码中创建 public 属性。如果您正在创建自己的 Simulink 块,那么在 MATLAB 系统对象中编写代码将非常友好 change/create 参数如您所愿。
Simulink 扩展系统对象可以创建如下:
要从 System object 创建 Simulink 块,请从现有 Simulink 块创建 "MATLAB system" 块,然后从 MATLAB 系统调用您的系统对象。
系统对象代码中的所有 public 属性在 Simulink 封装对话框中可见,如下图所示。
希望这就是您要找的。
以编程方式创建遮罩
我不确定这是否正是您要搜索的内容,但我在 MATLAB/Simulink 中举了一个关于如何通过脚本以编程方式创建掩码的示例。我不会使用 get_param
/set_param
,即使使用这些命令可以获得相同的结果。我们将使用更简单、更清晰(至少恕我直言)的 Simulink
对象。
对于我们的游乐场,让我们用一个简单的常量创建这个简单的子系统 (block
),该常量在输出中给出我们要从掩码中获取的名为 a
的变量的名称:
看看这个块的地址。我的 simulink 模型是 mask.slx
,因此我可以使用地址 mask/block
(视口的左上角)来寻址该子组,如您在此处所见:
此时我们可以使用如下代码为子组添加一个编辑参数框,固定a
的值:
clc
clear all
% The subgroup for which we want to programmatically create a mask
block_name = 'mask/block';
% Now we can create the mask parameter programmatically as you requested
% There are two way: the old one using get_param and set_param and a more
% clear one using the Simulink interface.
% I will go with thw second one, since it is really more straightforward
% with respect to the first one.
% The first think to do is to create the mask itself
% If the mask already exist, we would get an error, thus we can avoid it by
% checking if it already exist. This is something that you should check out.
mask_hdl = Simulink.Mask.create(block_name);
% mask_hdl = Simulink.Mask.get(block_name); % For use an existing mask
% Now we are ready to create the mask parameters:
edit_a_hdl = mask_hdl.addParameter( ...
'Type', 'edit', ...
'Prompt', 'Sets constant variable', ...
'Name', 'a');
edit_a_hdl.Value = '10';
运行 脚本将屏蔽代码并设置变量,如您在此处所见:
还有更多信息on this topic here。
以编程方式为屏蔽块设置参数
现在假设您像以前一样完成了游乐场,并且像上一张图片一样屏蔽了子组。您可以通过 get_param
和 set_param
以编程方式在掩码中设置它的值(或获取它),如下所示:
value = get_param(block_name, 'a');
value = str2double(value); % Values should always be string!
% Thus we must convert it
set_param(block_name, 'a', sprintf('%d', value * 100));
如您所见,值现已更新:
同样,您可以使用 Simulink
对象获得相同的结果。
mask_hdl = Simulink.Mask.get(block_name);
edit_a_hdl = mask_hdl.Parameters(1); % We know its position in the struct array
value = str2double(edit_a_hdl.Value);
value = value * pi;
edit_a_hdl.Value = sprintf('%f', value);
而且,如您所见,我们有了新值:
在这种情况下,我试图创建一个新的块参数作为新 属性 来保存我不想保存在已经为其他人保留的默认参数中的特定新数据不同的相关数据
对于这个参数我想使用命令get_param和set_para存在
我的意思是使用默认参数,那些。 https://edoras.sdsu.edu/doc/matlab/toolbox/simulink/slref/parameters2.html#7515
许多工具箱中的 Simulink 块是使用 MATLAB 系统对象创建的。如果要为现有 Simulink 块创建新参数,您可能需要在附带的 System object 代码中创建 public 属性。如果您正在创建自己的 Simulink 块,那么在 MATLAB 系统对象中编写代码将非常友好 change/create 参数如您所愿。
Simulink 扩展系统对象可以创建如下:
要从 System object 创建 Simulink 块,请从现有 Simulink 块创建 "MATLAB system" 块,然后从 MATLAB 系统调用您的系统对象。
系统对象代码中的所有 public 属性在 Simulink 封装对话框中可见,如下图所示。
以编程方式创建遮罩
我不确定这是否正是您要搜索的内容,但我在 MATLAB/Simulink 中举了一个关于如何通过脚本以编程方式创建掩码的示例。我不会使用 get_param
/set_param
,即使使用这些命令可以获得相同的结果。我们将使用更简单、更清晰(至少恕我直言)的 Simulink
对象。
对于我们的游乐场,让我们用一个简单的常量创建这个简单的子系统 (block
),该常量在输出中给出我们要从掩码中获取的名为 a
的变量的名称:
看看这个块的地址。我的 simulink 模型是 mask.slx
,因此我可以使用地址 mask/block
(视口的左上角)来寻址该子组,如您在此处所见:
此时我们可以使用如下代码为子组添加一个编辑参数框,固定a
的值:
clc
clear all
% The subgroup for which we want to programmatically create a mask
block_name = 'mask/block';
% Now we can create the mask parameter programmatically as you requested
% There are two way: the old one using get_param and set_param and a more
% clear one using the Simulink interface.
% I will go with thw second one, since it is really more straightforward
% with respect to the first one.
% The first think to do is to create the mask itself
% If the mask already exist, we would get an error, thus we can avoid it by
% checking if it already exist. This is something that you should check out.
mask_hdl = Simulink.Mask.create(block_name);
% mask_hdl = Simulink.Mask.get(block_name); % For use an existing mask
% Now we are ready to create the mask parameters:
edit_a_hdl = mask_hdl.addParameter( ...
'Type', 'edit', ...
'Prompt', 'Sets constant variable', ...
'Name', 'a');
edit_a_hdl.Value = '10';
运行 脚本将屏蔽代码并设置变量,如您在此处所见:
还有更多信息on this topic here。
以编程方式为屏蔽块设置参数
现在假设您像以前一样完成了游乐场,并且像上一张图片一样屏蔽了子组。您可以通过 get_param
和 set_param
以编程方式在掩码中设置它的值(或获取它),如下所示:
value = get_param(block_name, 'a');
value = str2double(value); % Values should always be string!
% Thus we must convert it
set_param(block_name, 'a', sprintf('%d', value * 100));
如您所见,值现已更新:
同样,您可以使用 Simulink
对象获得相同的结果。
mask_hdl = Simulink.Mask.get(block_name);
edit_a_hdl = mask_hdl.Parameters(1); % We know its position in the struct array
value = str2double(edit_a_hdl.Value);
value = value * pi;
edit_a_hdl.Value = sprintf('%f', value);
而且,如您所见,我们有了新值: