Embedded MATLAB FUnction as elaboration block only
정보
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
Hi,
I have to made an Embedded MATLAB Function which puts input in "for" and/or "if" loop(s) and takes output from loop conditions.
The real problem, infacts, is that Embedded MATLAB Function wants output DIRECTLY connected to input only.
Thanks for kindness
댓글 수: 1
Walter Roberson
2011년 12월 8일
There is no such thing as an if loop.
답변 (3개)
Kaustubha Govind
2011년 2월 23일
From what I understand, I think the real reason may be because you do not pre-allocate memory for the output. See Assign Variables Explicitly Before Using Them.
For example:
function X = fcn %#eml
N = 5;
X = zeros(1,N); % explicitly pre-allocate output
for i = 1:N
if mod(i,2) == 0
X(i) = i;
else
X(i) = 0;
end
end
If this is not the answer you are looking for, could you please post a simple code snippet so it is easier to understand the question?
댓글 수: 0
Kaustubha Govind
2011년 2월 24일
As I described before, you need to pre-allocate 'y' in your code. Since in this case, y is a scalar, you can simply have:
function y=fcn (s1, s2)
y = 0; % Pre-allocate y
m=length(s2);
n=length(s1);
for i=1:n
for j=1:m
if s1(i)==s2(j)
y=s1(i);
break
end
end
end
댓글 수: 4
cyberdyne
2011년 2월 24일
Kaustubha Govind
2011년 2월 24일
Unfortunately, it isn't possible to NOT have an output assigned. The best you can do is assign y=NaN, which gives you some indication of when s1(i) is not equal to s2(j).
cyberdyne
2011년 2월 24일
Kaustubha Govind
2011년 2월 25일
Sure, you could do that. Just make sure that you pre-allocate 'y' before the for-loops though.
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!