String class not recognized in S-function

I want to use an S-function in my simulink model to convert a constant integer to a string, using ASCII code also. The digits of the number will be assigned in the following way: (0-> 'A',...,9->'J') Ex.
input 123 -> S-function -> output 'BCD'. Basically, i would like the same thing as him http://www.mathworks.com/matlabcentral/answers/1804-ascii-string-with-sci-transmit
I'm using S-function builder. The code below is put in the Output pane
typedef unsigned char byte;
byte i,nr;
byte v[10];
char w[10];
char s; //sign of the input number
string Convert(u[0]); //the function which does the conversion
{
long newu = 0;
nr = 0;
s = 0;
if(u[0]<0)
s = 1;
while(u[0]!=0)
{
newu = newu * 10 + (u[0] % 10);
u[0]/=10;
nr++;
}
for(i=nr;i>=1;i--)
{
v[i]=newu %10;
newu/=10;
}
for(i=1;i<=nr;i++)
{
switch(v[i])
{
case 0: w[i]='A';
break;
case 1: w[i]='B';
break;
case 2: w[i]='C';
break;
case 3: w[i]='D';
break;
case 4: w[i]='E';
break;
case 5: w[i]='F';
break;
case 6: w[i]='G';
break;
case 7: w[i]='H';
break;
case 8: w[i]='I';
break;
case 9: w[i]='J';
break;
}
}
string Convert[0](w); // converts char array(w) into a string
return(Convert[0]);
}
In the Library pane, i include the following:
#include <string.h>, but after build, i get ''undeclared identifier `string'''
I don't know about the errors in the code(i'm sure there are many, because i have little c knowledge)

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 9 Jan. 2013

1 Stimme

The only type that <string.h> defines is size_t . See http://pubs.opengroup.org/onlinepubs/7908799/xsh/string.h.html
You might perhaps be thinking of C++'s <string;> . See http://www.cplusplus.com/reference/string/ but remember that is for C++ not for C.

4 Kommentare

Also, you need to have your Convert function in a separate file, and simply call it in the Outputs pane using something like:
out1 = Convert(in1);
Note: the names 'in1' and 'out1' might change depending on how you've named your ports in the Data Properties pane.
Zoltan
Zoltan am 10 Jan. 2013
Let me get this straight. I make a file which contains the function and it will have .c extension. Right? After this, i put the name of the file in the Libraries pane as a source file?
Walter Roberson
Walter Roberson am 10 Jan. 2013
If you use a .c extension then that would be for C language code, which does not define "string" as a datatype.
Kaustubha Govind
Kaustubha Govind am 10 Jan. 2013
Zoltan: Yes, that is correct.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by