Accessing struct variable data inside MATLAB function block in Simulink
52 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
Paul
2024-10-22,22:01
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 个评论
Paul
2024-10-23,23:45
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!