import csv using textscan but skip lines where column value meets user criteria

1 view (last 30 days)
I am wanting to import a csv using textscan where rows will be skipped if a column value meets a certain criteria.
Interval,Layer,Value,Comment
1,1,-78,ok
2,2,-89,ok
3, 0,-56,want this line gone
4,4,-34,ok
I wish to
This would reat the whole lot into data. I wish to exclude the third record where Layer = 0. Can this be done as a one-liner without having to resort to line-by-line reading ?
data = textscan(fid,'%f%f%fs','Delimiter',',')

Answers (2)

KSSV
KSSV on 2 Jul 2019
As the data int he file are numbers..you can import/ load the data and then you can remove the unwanted numbers using inequalitites.
REad about csvread, load, importdata and ineqalitites.
  2 Comments
Tim
Tim on 2 Jul 2019
no there are comment strings at the end, so not all numeric
Have found a solution by creating an index variable and applying to each variable in turn using a for loop. Reasonably efficient I guess but was thinking that there is a way to do this in a single line as part of the textscan function. .
idx = find(data{2}~=0); % locate all rows where second columne value is not zero
for i=1:length(data);
data{i}=data{i}(idx); % step through each column and apply idx to remove non-zero rows
end
KSSV
KSSV on 2 Jul 2019
YOu can read a file line by line as below:
fid = fopen(myfile);
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end
fclose(fid);

Sign in to comment.


Tim
Tim on 2 Jul 2019
That's right you can read line by line but this is inefficient especially for large files. Was wanting to just have a criteria for the textscan function to exclude lines where a condition is met and do it all in the one line. Thanks anyway KSSV.
  1 Comment
KSSV
KSSV on 2 Jul 2019
YOu can textscan the entire file into cells using textscan. Fin the required string using strcmp or contains. Remove those lines and write the data back to file.

Sign in to comment.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!