Taking data received from serial port as single element

Hi all,
i am receiving the ASCII values of some data say (*12) through serial port, then after that i am eliminating first character i.e(*)Then i am converting this ASCII values to Decimal values using 'char'. Later i need to process remaining data '12' as single element for giving it as an input to some other function. But problem is my function is taking '1' and '2' as separate values instead of one single value '12'. please give some suggestions in a way such that i should give '12' as input to my function.
Sample Code
ser1 = serial('COM27', 'BaudRate', 9600,'Terminator','*');
fopen(ser1);
while(1)
input3=fread(ser1);
input1=input3(2:end)
input2=char(input1)
input3=input2
output=input3+1;
end
If input is *12 expected output is 13. But i am getting output as 2(1+1),3(2+1)
Thanks in advance

 采纳的回答

You don't want to use CHAR, but SSCANF or STR2NUM, to convert your char input1 into a numeric variable.
>> input1 = '12' ;
>> class(input1)
ans =
char
>> str2num(input1)
ans =
12
>> class(ans)
ans =
double
>> sscanf(input1, '%d')
ans =
12
>> class(ans)
ans =
double

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by