Can I add parameters to a MATLAB function block programmatically?

3 次查看(过去 30 天)
Dear community,
I want to use my base workspace variables as parameters in my Matlab function block in Simulink. If possible, I want to use as little code as possible inside the Matlab function block. My current solution is:
  1. Create a struct in the base workspace and add all variables to it via the dot-operator.
  2. Add the struct (let´s call it 'P') via 'Edit Data' in the 'Ports and Data Manager' as Input and change the scope to 'Parameter'.
  3. Re-assign the parameters inside the function block manually, such as:
a = P.a;
b = P.b;
[...]
After searching the forums for hours, I have not found a better solution that I understand. Is there a way to use the parameters without re-initializing them inside the Matlab function body? I know that I could add them individually as Parameters via 'Edit Data' just as I did with the struct, but there are too many.
PS: I cam all the way from saving a .mat file in the base WS and loading it to a handle in the function body. I wish the function to contain as little extra code as possible, only the "core computations" shall remain, so to say.
  1 个评论
darova
darova 2020-5-4
I'm interested in this question too
Meantime, you can extract variable using for loop
function main
s.a = 1;
s.b = 2;
s.c = 3;
[a1 b1 c1] = myassign(s)
function varargout = myassign(s)
if nargout ~= length(fieldnames(s)) % if number of arguments does not match
disp('error')
varargout = cell(nargout,1); % assign empty cells
return
end
fnames = fieldnames(s);
for i = 1:length(fnames)
varargout{i} = getfield(s,fnames{i});
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by