Delete line of file based on number in first column

조회 수: 2 (최근 30일)
Henrik Larsen
Henrik Larsen 2016년 9월 5일
댓글: arvind ramasamy 2018년 7월 11일
I'm trying to delete lines in a file based on a number in the first "column" of the file. The file(s) looks something like:
8 Job Operator Date Time Instrument
8 15899-2016 THP/KVE 16-08-11 08:13:09 1626136
8
8 PT Code y X Z
6 CKOTE1 250 137827.548 98704.895 32.880
6 CKOTE2 250 137828.245 98707.396 32.880
6 CKOTE3 250 137830.581 98711.226 32.880
8 CKOTE4 250 137833.055 98713.677 32.879
So my questions is how i tell matlab to skip lines containing the char "8" in the first column, so I get the remaining data:
6 CKOTE1 250 137827.548 98704.895 32.880
6 CKOTE2 250 137828.245 98707.396 32.880
6 CKOTE3 250 137830.581 98711.226 32.880
Thanks

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 9월 5일
fid=fopen('file.txt');
l1=strtrim(fgetl(fid));
out={};
while ischar(l1)
l2=strtrim(l1);
if l2(1)~='8'
out{end+1,1}=l1;
end
l1=fgetl(fid);
end
out
  댓글 수: 2
Henrik Larsen
Henrik Larsen 2016년 9월 8일
I don't know if I need to make a new question, anyway here goes:
I can't make it work if forexample i just make an empty line like this:
8 Job Operator Date Time Instrument
8 15899-2016 THP/KVE 16-08-11 08:13:09 1626136
8 PT Code y X Z
6 CKOTE1 250 137827.548 98704.895 32.880
6 CKOTE2 250 137828.245 98707.396 32.880
6 CKOTE3 250 137830.581 98711.226 32.880
8 CKOTE4 250 137833.055 98713.677 32.879
This will just give me an out of bound error, I have tried to work it out myself but I just can get it to work.
Any suggestions?
arvind ramasamy
arvind ramasamy 2018년 7월 11일
I make a serial communication with an arduino and get the data of accelerometer mag and gyro values my data comes in as a string. 's' is the object i created for serial communication
data =fscanf(s,%s) % gives me the data from the arduino
i want my vector size to be 500. so when I run it in for loop
for i= 1:50
data =fscanf(s,'%s');
end
my output is: data = initializationsuccesful!
data = ax:123ay:23az:1234gx:23gy:50gz:43mx:23my:123mz:234
and goes on.
I use,
A_x(:,i) = str2num(data(:,4:8))
to get my vector. but since 'initializationsuccessful!' is not a number i am unable to form my vector. so how can I do it ???? this is my question

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by