Accessing struct variable data inside MATLAB function block in Simulink

In MATLAB, I've been successfully using a custom matlab function that pulls data from a (complicated?) struct variable. The struct variable is one of the inputs in the function call. This struct variable is a 1x21 struct with 48 fields. The fields include strings, 1x1 doubles, nxm double arrays, griddedInterpolant classes. Note that all values in the struct are constants and are used for lookups and calculations.
I'm trying to recreate this same function in Simulink using the MATLAB function block. I'm at a loss to how I get the block to access the struct variable, which is in my workspace. I think the difficulty may come from the fact the struct has a bunch of different classes. I've tried having an input port on the function block that gets the data from a From File Block. I've tried a global variable, and I've tried loading the data inside the MATLAB function block. None of these seem to work.
Any suggestions on how I can get the data stored in this struct variable accessible inside the Simulink MATLAB function block? Thank you.

 Accepted Answer

Hi Jacob,
The typical way to define a parameter in a Matlab Function block is described at Use Data in Multiple MATLAB Function Blocks by Defining Parameter Variables. However, I suspect that won't work because your struct has properties and field data that aren't supported. If it doesn't work, then I guess you can use the Matlab Function block as a "wrapper" that calls your actual function, with your actual function declared using coder.extrinsic in the Matlab Function, which should work as long as you don't require code generation. There would be a few options to get your structure into the actual function.

3 Comments

Paul, thank you for the suggestion. The wrapper option makes sense to me.
One quick follow up. I've given it a try and while I've found a temporary solution, I feel like it's not ideal. When I declare the matlab function as extrinsic and run in Simulink, the function cannot find the struct variable on my workspace.
So the following Matlab function block in Simulink does not work:
function [out1,out2] = myfunc_simulink(in1,in2)
coder.extrinsic('myfunc_matlab')
[out1,out2] = myfunc_matlab(in1,in2,mystruct)
end
"Undefined function or variable mystruct."
If I instead modify my matlab function to load the missing "mystruct" variable from a .mat file during the function call, Simulink does run successfully. The matlab function block in Simulink looks like this:
function [out1,out2] = myfunc_simulink(in1,in2)
coder.extrinsic('myfunc_matlab')
[out1,out2] = myfunc_matlab(in1,in2)
end
and the matlab function looks more like this:
function [out1,out2] = myfunc_matlab(in1,in2)
load('mystruct.mat')
etc. etc. etc.
end
However, I feel like this is slowing down the simulation by loading the .mat file over and over again. Any thoughts on speeding it up?
Thanks again!
Jacob
If you want to pull in mystruct from the base workspace, and your function myfunc_matlab takes in mystruct as the the third argument, then a quick (and dirty, if not the dirtiest) approach could be:
% Matlab function block in Simulink
function [out1,out2] = myfunc_simulink(in1,in2)
coder.extrinsic('myfunc_wrapper');
out1 = % assign a dummy value here to out1 so that the coder knows it's type and dimensions
out2 = % assign a dummy value here to out2 so that the coder knows it's type and dimensions
% there will be an issue if size/type of out1/2 returned from
% myfunc_wrapper changes during the run.
[out1,out2] = myfunc_wrapper(in1,in2)
end
Then define an m-fucntion in a file that pulls in mystruct and then calls the real function.
function [out1,out2] = myfunc_wrapper(in1,in2)
mystruct = evalin('base','mystruct');
[out1,out2] = myfunc_matlab(in1,in2,mystruct);
end
There might be better alternatives depending on how you construct mystruct in the first place, and, frankly, there might be a better alternative in general, though none comes to mind at present.
Paul, this worked great. The model runs considerably faster this way. Thank you for the insight.
Jacob

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2024b

Asked:

on 22 Oct 2024

Commented:

on 25 Oct 2024

Community Treasure Hunt

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

Start Hunting!