using ismember function in simulink

조회 수: 9 (최근 30일)
Juan Nunez
Juan Nunez 2018년 11월 3일
댓글: Juan Nunez 2018년 11월 6일
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