How can i use the 'mod' function in an Embedded Matlab function?

I have the following function. I want to use this algorithm in an embedded Matlab function in simulink.
function y = fcn( u )
y=[0;0;0;0;0;0;0;0;0;0];
newu=0;
nr=0;
s=false;
if u<0 s=true;
end
while u~=0
newu=newu*10+mod(u,10);
u=(u/10);
nr=nr+1;
end;
for i=nr:0
y(i)=mod(newu,10);
newu=(newu/10);
end;
for i=0:nr
y = y+65;
end
end
After compilation it gives me the error :'Function 'mod' is not defined for values of class 'embedded.fi'. 'u'(input value) being of class 'embedded.fi'. What do i have to change to get my output value?
Thank you, Zoli

15 Comments

Note:
for i=nr:0
is not going to execute any loops when nr > 0.
Note: if u < 0 then you set s, but you do not do anything with s and you do not change the sign of u; are you sure you are going to get the right answer? Perhaps you want nr:-1:0 ? But then how are you going to store a value at y(0) ?
Note: y = y + 65 is going to add 65 to every member of y. And you have that in a loop...
I think he wants for i=nr:-1:0
Probably wants to count backwards, yes, but still has problems at y(0)
nr is the number of digits, but nr:-1:0 is looping nr+1 times.
yes, i want to count backwards (downto- in pascal) and i don't know the equivalent in Matlab.
Syntax is
for i = nr: -1: 0
but please re-check whether you want to stop at 0 or at 1.
at 0...but my question is, can i use mod with the u variable which is of class'embedded.fi'? Because it doesn't let me use it. Can i store u in another type of variable, changing the class?
*correction
for i = nr: -1: 0
y(i)=mod(newu,10);
newu=(newu/10);
end;
for i=0:nr
if y(i)==0 then y(i)=65;
end;
if y(i)==1 then y(i)=66;
end;
if y(i)==2 then y(i)=67;
end;
if y(i)==3 then y(i)=68;
end;
if y(i)==4 then y(i)=69;
end;
if y(i)==5 then y(i)=70;
end;
if y(i)==6 then y(i)=71;
end;
if y(i)==7 then y(i)=72;
end;
if y(i)==8 then y(i)=73;
end;
if y(i)==9 then y(i)=74;
end;
end
Look at your code again, what you show above. In the two "for" loops, where is the data being stored when "i" is 0 ?
oh...you mean i can't use 0 as an index?
Correct. You cannot use 0 as an index. But you probably don't want to: you are only manipulating nr values, not nr+1 values, so 1:nr and nr:-1:1 would seem to be appropriate.
OK, so i understand how to use syntax correctly. I wouldn't need this algorithm if i would be able to send values through RS-232 to my microcontroller...but for some reason i get the same sent value back only in the 64..127 range. for example, if i send 0, i receive -192 on the scope in my simulink model. why is this? thanks
Double-check the parity of the line. But even more so, double check how the heck an 8 bit result is generating a negative number below -128, as 8 bit signed values can only be in the range -128 to +127 . Check your scope settings.

Sign in to comment.

Answers (1)

In the below part of your code
while u~=0
newu=newu*10+mod(u,10);
u=(u/10);
nr=nr+1;
end;
newu increases rapidly to infinity. that's what causes a problem when
y(i)=mod(newu,10);

6 Comments

why does it increase to infinity?
try this
u=1,newu=0;nr=0
while u~=0
newu=newu*10+mod(u,10);
u=(u/10);
nr=nr+1;
end;
newu
Remember, Azzi, the input is fixed point, so the division is going to truncate at some point.
Yes, but after 309 iterations, 10^309=inf
In the above example, nr=324
This is only a problem if the fi object has a representation for infinity and if the input is infinity. Otherwise abs(u) is diminishing in each step and there would be a finite end to the process. It could potentially overflow the buffer of 10 y locations, but that would depend upon the maximum value possible for that particular fi object.

Sign in to comment.

Categories

Products

Asked:

on 17 Dec 2012

Community Treasure Hunt

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

Start Hunting!