Delete line of file based on number in first column

2 次查看(过去 30 天)
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 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by