"Subscripting into an mxArray is not supported" error in Simulink User Defined Function due to 'fieldnames' extrinsic call

1 次查看(过去 30 天)
Although I look around the same question on web and see the reasons, I can not find a way to get rid of this error.
I got the error 'Subscripting into an mxArray is not supported' while using Simulink-User Defined Function. My 'extrinsic' call is fieldnames. How can I assign fieldNames(capital 'N', fieldNames=fieldnames(data)) prior to that call? How can I tell the types of fieldNames are strings without subscripting into an mxArray? Because I cannot use fieldNames(i)=...
Note: Or any other suggestion without cell array, fieldname etc? More specifically, I have a .mat file, variable names and their values in it. I will use not only the values, but also their names (strings or char) in my calculations. That is, somehow I should access variable names and values in my Simulink-User Defined Function
function damping = fcn(u)
%#codegen
coder.extrinsic('fieldnames');
coder.extrinsic('length');
coder.extrinsic('cell');
coder.extrinsic('cellstr');
coder.extrinsic('string');
numberOfHydroData = 0;
i = 0;
stateProducts = zeros(10,1);
fieldName = ' ';
for k=1:1:13
for kk=1:1:5
% fieldNames(k,kk)=' '; %if uncomment, will give error
end
end
%%
hydroData = load('data.mat');
hydroData = hydroData.hydroData; % see image below to see how hydroData looks like after implementing this line
numberOfHydroData = length(fieldnames(hydroData))
%% calculation
hydroForces = zeros(6,1);
damping = zeros(6,1);
stateProducts = zeros(int8(numberOfHydroData),1);
fieldNames = cellstr(fieldnames(hydroData));
for i=1:1:numberOfHydroData
fieldNames(i) % just for test below code
% fieldName = char(fieldNames((i))); % if uncomment, will give error
end
% damping = [0;0;0;0;0;0];
damping = hydroForces;
%%
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by