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

Respuestas (3)

Kaustubha Govind
Kaustubha Govind el 23 de Feb. de 2011
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?
cyberdyne
cyberdyne el 23 de Feb. de 2011
The problem is this: I've two changing inputs to EML; call them s1 and s2. They are two definite integrals each of them from t0 to t0+Ts (where Ts is clock time and t0 is the starting istant).Therefore they are two successions of numbers. I want that when this two inputs have the same value, output is that value. I try with following code:
function y=fcn (s1, s2)
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
but EML wants that y must be ALWAYS directly bind to inputs. It doesn't want loop conditions.
Kaustubha Govind
Kaustubha Govind el 24 de Feb. de 2011
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 comentarios

cyberdyne
cyberdyne el 24 de Feb. de 2011
But in this way while s1(i) is not equal to s2(j), y=0 continuously. Instead i want that there is an output when s1==s2 only.
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
cyberdyne el 24 de Feb. de 2011
Therefore if i take as "else" condition:
else y=NaN;
it means that when s1 is not equal to s2 it returns NaN-elements ?
Sure, you could do that. Just make sure that you pre-allocate 'y' before the for-loops though.

La pregunta está cerrada.

Preguntada:

el 23 de Feb. de 2011

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by