问题描述
我想创建一个简单的Simulink模型,其中包含"MATLAB函数"以编程方式进行阻止-即使用Matlab代码.
I'd like to create a simple Simulink model containing a "MATLAB Function" block programmatically -- i.e. using Matlab code.
感谢本指南,我设法创建了一个包含该块的新模型:
Thanks to this guide, I've managed to create a new model containing the block:
open_system(new_system('my_system'))
add_block('simulink/User-Defined Functions/MATLAB Function', 'my_system/my_func')
通常,为了编辑"MATLAB功能"块的代码,必须双击该块然后输入新代码来打开"该块.
Usually, in order to edit the "MATLAB Function" block's code, one has to "open" the block by double-clicking on it then entering the new code.
但是,我想使用例如 set_param()
或任何相关功能.
However, I would like to set that code programmatically using e.g. set_param()
or any relevant function.
例如,将以下内容设置为块的代码:
For instance, to set the following as the block's code:
function y = fcn(v)
%#codegen
y = 2 * u;
我想使用类似的东西:
set_param('my_system/my_func', 'Script',...
'function y = fcn(u)\n%#codegen\n\ny = 2 * u;'...
);
我已经查看了get_param('my_system/my_func', 'ObjectParameters')
的输出,并试图猜测可能使用哪个参数来设置块的功能代码:到目前为止,我找不到任何参数.因此,我的问题是:
I've looked at the output of get_param('my_system/my_func', 'ObjectParameters')
and tried to guess which parameter might be used to set the block's function code: So far, I couldn't find any. Therefore, my question is:
问:是否可以仅使用Matlab命令在Simulink中设置"MATLAB功能"块的代码?
推荐答案
(按照@Ander Biguri的要求,我已将对我有用的 a 解决方案移到了单独的答案中.如果有人有替代/更好的方法,请随时发布它)
(As requested by @Ander Biguri, I've moved a solution that worked for me to a separete answer post. If anyone has an alternative/better approach, please feel free to post it as well)
好吧,似乎这个问题是在此处之前问到的. (虽然表达可能有所不同吗?)我设法使用以下代码解决了我的问题:
Well, it seems that this question was asked here before. (perhaps formulated differently though?) I've managed to solve my issue using the following code:
sf = sfroot()
block = sf.find('Path','my_system/my_func','-isa','Stateflow.EMChart');
block.Script = sprintf('function y = fcn(u)\n%%#codegen\n\ny = 2 * u;')
这篇关于编辑"MATLAB功能"的代码.以编程方式阻止Simulink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!