using ismember function in simulink

9 次查看(过去 30 天)
Hello Guys,
Below you can see a very simple function I'm using in Simulink. When I try to run the simulation, the following error pops up (u1 and u2 are constants):
Function output 'Result' cannot be an mxArray in this context. Consider preinitializing the output variable with a known type.
Function 'MATLAB Function11' (#138.81.87), line 2, column 10:
"Result"
Any ideas??
Thanks.
function out = fcn(u1,u2)
coder.extrinsic('ismember');
Result=ismember(A,[u1 u2],'rows');
out=Result(Result==1);

回答(1 个)

Walter Roberson
Walter Roberson 2018-11-3
function out = fcn(u1,u2)
coder.extrinsic('ismember');
out = zeros(size(A, 1),1);
[~, out] = ismember(A,[u1 u2],'rows');
This version assumes that multiple rows might match, and assumes that you were looking for the index of the match, not a vector of logical true the length of the number of matches, empty for no match. If there is always definitely exactly one match then initialize out to 0.
If one match is what is expected then you should rewrite your code to avoid ismember since code cannot be generated for ismember.
Result = logical(size(A, 1),1);
Result = A(:, 1)==u1 & A(:, 2)==u2;
And then that convert that into the desired output, such as
out = 0;
out = find(Result, 1);
  1 个评论
Juan Nunez
Juan Nunez 2018-11-6
Thank you for answer Walter. I'm sorry I didn't post a comment sooner. I used the first approach and it worked.

请先登录,再进行评论。

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by