Check two string data without carriage return symbol

조회 수: 3 (최근 30일)
Sergey Makovkin
Sergey Makovkin 2016년 3월 25일
편집: Ced 2016년 3월 25일
Hi all!
In the serial com port channel values comes from 0 to 1023, this example from Arduino: https://monosnap.com/file/TiTehc6zWhu0zszPnCALbnjIMAfpzf
On the MATLAB side I parse it via function:
while(obj1.BytesAvailable)
Psvoltage = deblank(fscanf(obj1)) %, '%d', 4)
[a, b] = size(Psvoltage)
bb = findstr(Psvoltage, '\r')
if ~isempty(Psvoltage)
sPs = str2num(Psvoltage) * (100 / 1023); %
break;
end
end
But sometimes from serial port I take corrupted data like this: https://monosnap.com/file/ysJFdabTbOVOKoQbgqJYF44SioAORy
And it is cause error in my program:
https://monosnap.com/file/oEJlyjnifsTx6h531xfDi23nQ89Shv
https://monosnap.com/file/6fdI8mgQa6pVjKn2EHFqzXdQnDDLzb
This is two string data, but without carriage return symbol, because all checking such as: ~isnan(), ~isempty() and findstr(Psvoltage, '\r') this data had been passed.
The question is: how do I check these data would be to ignore them?
Thank you!

채택된 답변

Ced
Ced 2016년 3월 25일
편집: Ced 2016년 3월 25일
Why not just take the first row?
I.e.
Psvoltage = deblank(fscanf(obj1)) %, '%d', 4)
if ( ~isempty(Psvoltage) )
Psvoltage = Psvoltage(1,:);
end
edit: added ~isempty check
  댓글 수: 2
Sergey Makovkin
Sergey Makovkin 2016년 3월 25일
I tried you case, but it dont solve my problem with corrupted data. This is the code: https://monosnap.com/file/qgg8o3x6aQ2rbs6AtFpGsNh5LEtXZO
This is corrupted case with errors: https://monosnap.com/file/rAr5kgsKK2OAtVCKLNKiXL995Gffzb
Ced
Ced 2016년 3월 25일
편집: Ced 2016년 3월 25일
How about
Psvoltage = deblank(fscanf(obj1)) %, '%d', 4)
if ( ~isempty(Psvoltage) )
volt_num = str2num(Psvoltage);
Psvoltage = volt_num(1);
end
?
or simply < removed, did not work >, see Sergey's answer below.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sergey Makovkin
Sergey Makovkin 2016년 3월 25일
Thank you very much! First variant is working correctly, but second is not (please see the 2nd result: https://monosnap.com/file/6XxSZBS1pNI2AcPHIZNSq3n4SCSPJD)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by