using ismember function in simulink

10 ビュー (過去 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 Center および File ExchangeString についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by