How can I import my large text file in MATLAB
My file has a header and the a header of each col and then the cols. I'm interested in pulling out the data from just the 4th col into a MATLAB variable.
Ive run into a few problems. One is that there are about 3 million cells and MATLAB truncates this for some reason when I use the import wizard (it truncates the file to 191,686 rows).
Here is the first few lines of the file:
Channels: 1
Count: 3600000
Start: 40640.854055 04/07/11 16:29:50
End: 40640.895721 04/07/11 17:29:50
Date Time Time Stamp Time from Start EEG 1_8401
04/07/11 16:29:50 40640.687388 0.000000 3.854626e+001
04/07/11 16:29:50 40640.687388 0.001000 2.664706e+001
04/07/11 16:29:50 40640.687388 0.002000 1.425481e+001
04/07/11 16:29:50 40640.687388 0.003000 1.704369e+000
04/07/11 16:29:50 40640.687388 0.004000 -1.070827e+001
04/07/11 16:29:50 40640.687388 0.005000 -2.290569e+001
Here is the script that I used to import the file when it was truncated:
function importfile(fileToRead1)
DELIMITER = '\t';
HEADERLINES = 6;
% Import the file
newData1 = importdata(fileToRead1, DELIMITER, HEADERLINES);
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
I was thinking of doing something like this with textscan
but am not sure how to skip开发者_高级运维 the headers doing that and keep getting buffer overflow errors:
fid = fopen('scan1.dat');
C = textscan(fid, '%*d/%*d/%*d %*d:%*d:%*d %*f %*f %f')
fclose(fid);
Here's one solution using TEXTSCAN:
fid = fopen('scan1.dat','r');
dataCell = textscan(fid,'%*s %*s %*f %*f %f','HeaderLines',6);
fclose(fid);
The first 6 lines are ignored using the 'HeaderLines'
parameter, the date and time are read as strings and ignored (a little more compact than reading them as integers and ignoring them), and the first two columns of floating point data are ignored. The contents of dataCell{1}
will be the last column of your data.
精彩评论