Filter löschen
Filter löschen

Can somebody tell me what is wrong with the following code?

1 Ansicht (letzte 30 Tage)
Zoltan
Zoltan am 17 Dez. 2012
Hi,
I wrote the following function to build an array of numbers(in the range of 65...74), representing the digits of a number. Here's the algorithm:
function [ y ] = fcn( u )
var y:array[1..10] of byte;
u: longint;
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+mod(u,10);
u=(u/10);
inc(nr);
end;
for i=nr:1 do
begin
y(i)=mod(newu,10);
newu=(newu/10);
end;
for i=1:nr do
begin
if y(i)==0 then y(i)=65;
if y(i)==1 then y(i)=66;
if y(i)==2 then y(i)=67;
if y(i)==3 then y(i)=68;
if y(i)==4 then y(i)=69;
if y(i)==5 then y(i)=70;
if y(i)==6 then y(i)=71; //At least one END is missing: the statement may begin here.
if y(i)==7 then y(i)=72;
if y(i)==8 then y(i)=73;
if y(i)==9 then y(i)=74;
end
end
y=u;
end
And Matlab gives me the error message stated after the 2 slashes...I am a beginner in Matlab, i can only write code in Pascal language, so I would be glad if someone helped me out.
Thank you, Zoli
  1 Kommentar
Matt Fig
Matt Fig am 17 Dez. 2012
It helps to read the Getting Started section of the doc before jumping in like this. You need to understand basic syntax.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Pedro Villena
Pedro Villena am 17 Dez. 2012
num2str(u)
doc num2str

Weitere Antworten (1)

John Petersen
John Petersen am 17 Dez. 2012
You're syntax is incorrect for Matlab code. First, you don't need to declare your variables. Take out the do's, begin's, and then's. You need an end after every if statement. Comments begin with %, not //. The last for loop can be simplified to
y = y + 65;
  2 Kommentare
Zoltan
Zoltan am 17 Dez. 2012
Thank you for the advice
Zoltan
Zoltan am 17 Dez. 2012
I rewrote the code. I want to use this as an embedded function in simulink. Everything is OK, except it doesn't wanna use the 'mod' function with the input value(u), because its class is embedded.fi. How can i resolve this problem? Thanks

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by