Can I add parameters to a MATLAB function block programmatically?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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:
- Create a struct in the base workspace and add all variables to it via the dot-operator.
- 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'.
- 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 Kommentar
darova
am 4 Mai 2020
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
Antworten (0)
Siehe auch
Kategorien
Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!