Can i convert the following code into Matlab embedded function(for simulink use)

3 views (last 30 days)
Hello,
I would like to write the matlab equivalent of the following code(pascal) into a Matlab embedded function, then put this block into my simulink model. I need all this mess because i want to send a value via RS-232 from simulink to a microcontroller, and for some reason, only the values in the range of 65..127 are received correctly by the uC.
I have 2 questions:
1. is it correct to use matlab embedded function
2. if yes, could somebody help and send me the converted code
Here is the pascal code:
var y:array[1..10] of byte;
u: longint; //input number
i,nr:byte;
s:boolean;
begin
newu:=0;
nr:=0;
s:=false;
if u<0 then s:=true;
while u<>0 do
begin
newu:=newu*10+(u mod 10);
u:=u div 10;
inc(nr);
end;
for i:=nr downto 1 do //puts the digits of input number into string
begin
y[i]:=(newu mod 10);
newu:=newu div 10;
end;
for i:=1 to nr do
begin
case y[i] of
0: y[i]:=65;
1: y[i]:=66;
2: y[i]:=67;
3: y[i]:=68;
4: y[i]:=69;
5: y[i]:=70;
6: y[i]:=71;
7: y[i]:=72;
8: y[i]:=73;
9: y[i]:=74;
end;
end.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Dec 2012
If I scan the pascal correctly, then you might be able to use something like
y = sprintf('%d', u) - '0' + 'A';
nr = length(y);

More Answers (1)

Ryan G
Ryan G on 12 Dec 2012
I'm not sure if this will help but there is a RS232 Blockset on the file exchange you should probably check out.
I try to avoid MATLAB functions in Simulink whenever possible. Sometimes you may do something that doesn't generate c-code well in the MATLAB function and then you'll get stuck when you go to deploy the controller.
I would suggest looking at the blockset to see if you can workaround using this code, if not comment back and edit the snippet above to be a bit more readable. See here

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!