Using code to import data

6 views (last 30 days)
rmh
rmh on 23 Jul 2015
Commented: Star Strider on 23 Jul 2015
I have text files that I am trying to import into MATLAB (sample attached). The text files contain data collected using sensors (named q01, q02 etc.). The first 5 lines are garbage all I really need are the lines of data after q01 and q02 (complete file has more sensors). I have 529 of these files that I am trying to read in so it is not practical to use the import tool each time. I have the file names stored in a structure (field=names). This is the code that I have tried to use to read in the data but it is not extracting all of the data.
delimeterIn=' ';
headerlinesIn=5;
%Imports data and creates a structure with separate fields for numerical
%and text data
for i=1:length(files)
x(i)=importdata(files(i).name, delimeterIn, headerlinesIn);
end
Any help efficiently reading these files in would be appreciated.

Accepted Answer

Star Strider
Star Strider on 23 Jul 2015
If the files are all the same format, use the textscan function. No file was attached, but if one magickally appears, I will do my best to help you import the data from it. (It is possible that there are subsequent header lines embedded in the file, and that may be the reason you are not able to import the entire file. There are ways to deal with that with textscan.)
  3 Comments
rmh
rmh on 23 Jul 2015
Just as further clarification the data I need is the last number in the first line. A string vector with all the sensor names (q01, q02, etc.) and then the ten number matrix after each sensor name. I can ignore the three numbers in the line immediately after the sensor name. Thanks!
Star Strider
Star Strider on 23 Jul 2015
This works for the file you posted. You can get the information from the data it returns. You may have to experiment with the code, but to the best of my knowledge, this returns everything you want. All the files have to have exactly the same structure and format, or the code will break.
The code:
NFiles = 1;
for k1 = 1:NFiles
fidi = fopen('rmh Sample File.txt','rt');
lastnrfirstline{k1} = textscan(fidi, '%*f%*f%*f%f', 'HeaderLines',0, 'CollectOutput',1, 'EndOfLine','\r\n');
[sensr{k1,1}, posn] = textscan(fidi, '%3c\r\n', 'HeaderLines',4, 'CollectOutput',1, 'EndOfLine','\r\n');
fseek(fidi, posn, 1);
[data1{k1,1}, posn] = textscan(fidi, '%f%f%f%f%f', 'HeaderLines',2, 'CollectOutput',1, 'EndOfLine','\r\n');
fseek(fidi, posn, 1);
sensr{k1,2} = textscan(fidi, '%3c\r\n', 'HeaderLines',0, 'CollectOutput',1, 'EndOfLine','\r\n');
fseek(fidi, posn, 1);
data2{k1,2} = textscan(fidi, '%f%f%f%f%f', 'HeaderLines',2, 'CollectOutput',1, 'EndOfLine','\r\n');
fclose(fidi);
end
FirstLineLastNr = lastnrfirstline{1} % Display Last Number Of The First Line
sensname1 = (sensr{1,1}) % Display Sensor #1 Name
sensname2 = (sensr{1,2}) % Display Sensor #2 Name
S1 = cell2mat(data1{1}) % Display ‘Sensor #1’ Data For File #1
S2 = cell2mat(data2{2}) % Display ‘Sensor #2’ Data For File #1
It looks a bit kludgy, but it works, and returns the last number of the first line, and the sensor names, as well as the data. The ‘Display’ lines are there to verify the routine does what it’s supposed to do, and the best method to access the information the routine returns. They are otherwise not necessary for the code.
It’s designed to handle multiple files, but with only one to work with, I can only experiment with one.

Sign in to comment.

More Answers (0)

Categories

Find more on Text Analytics Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!