I have a large number of .CSV files in one folder. These files have different columns of data. I need to locate the desired columns by matching the header(string) and read only these columns. Example:
Date Time Temp_F Temp_C Lights_ON A/C_ON
5/23/15 12:23:07 70 21 0 1
5/23/15 12:23:08 71 22 0 1
5/23/15 12:23:09 71 22 1 1
Date Time Temp_F Temp_C Temp_K Lights_ON A/C_ON
5/24/15 13:30:27 63 17 290 1 0
5/24/15 13:30:28 64 18 291 0 0
5/24/15 13:30:29 65 18 291 1 0
I'd like to read Time, Temp_C and Lights_ON from these two files.
Before, the data was the same in all the files. I knew which columns to read and which columns to skip and was using the following code:
c = textscan('%*s%s%*d%d%d%*d', 'Delimiter', ',', 'Headerlines', 1);
Now I need to sort through the headers and match the strings and only read those columns. How would I go about doing this? Can I use textscan to accomplish this? Or maybe csvread or another command? I have attached a sample .CSV file. Thanks in advance.

 채택된 답변

Star Strider
Star Strider 2015년 5월 24일
편집: Star Strider 2015년 5월 25일

1 개 추천

This is one way to approach it:
[n,s,r] = xlsread('Jacee Johnson Book1.csv');
des_cols = {'Time', 'Temp_C', 'Lights_ON'};
colhdrs = s(1,:);
[~,ia] = intersect(colhdrs, des_cols);
colnrs = flipud(ia);
Data = n(:, colnrs-1);
It first reads the file in, then compares the column headers to your desired strings to find the desired columns in ‘colnrs’. Since ‘n’ here is a double array, it then selects the desired columns by numbers, and returns them in ‘Data’. The ‘n’ variable does not include the date (a string), so I subtracted 1 from those columns so they would match appropriately. The time (in Data(:,1) here) is in fractions of a day.

댓글 수: 4

Walter Roberson
Walter Roberson 2015년 5월 24일
I think you wanted [~,ia] as your output, not [~ia]
Star Strider
Star Strider 2015년 5월 25일
Corrected now. Did a copy-paste from working code. Don’t know how the comma got dropped.
Jacee Johnson
Jacee Johnson 2015년 5월 25일
Thank you very much! This method seems like it will work!
Star Strider
Star Strider 2015년 5월 25일
My pleasure!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 5월 24일

0 개 추천

Consider using the relatively new readtable() as that allows you to address content by column name.

댓글 수: 1

Jacee Johnson
Jacee Johnson 2015년 5월 25일
I was reading about it but unfortunately I only have access to MATLAB 2011. Thanks for the help.

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

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

질문:

2015년 5월 24일

댓글:

2015년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by