How to read in a csv file with uneven columns?

11 views (last 30 days)
My csv file has two different types of rows:
Sep 30, 2016 15:45:09.486686950 BST 00:00:00:aa:00:df ARP 44
Sep 30, 2016 15:45:09.486688819 BST 00:00:00:aa:00:df ARP 44
Sep 30, 2016 15:45:09.486697770 BST 10.0.0.33 53830 10.0.1.35 9000 TCP 76
Sep 30, 2016 15:45:09.486711587 BST 10.0.0.33 53830 10.0.1.35 9000 TCP 76
While the date and time columns are consistent, the rest are not. In some rows, there are 4 columns containing IPv4 address and port numbers (source and destination) before the protocol column. In other rows, there is only 1 column containing a MAC address. I've tried to import the data using space delimiters but they are not very consistent as the destination IP addresses seem to fall under two or three different columns themselves because of multiple whitespaces. Is there any easy way to read this file correctly?

Accepted Answer

Image Analyst
Image Analyst on 17 Oct 2016
You might have to read each line one at a time with fgetl() and then parse that line differently with textscan() or sscanf() depending on the length of the line you retrieved
fid = fopen('fgetl.m');
thisLine = fgetl(fid);
while ischar(thisLine)
disp(thisLine)
thisLine= fgetl(fid);
if length(thisLine) > 50 % (or whatever the short length is)
% Parse one way
else
% parse the other way
end
end
fclose(fid);

More Answers (0)

Categories

Find more on Data Import and Analysis 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!