Ben B.
Ben B.

Reputation: 97

Using importdata() to automatically change headerlines from file to file

Okay so I have multiple files that I'm trying to import for analysis, and I'm using uiimport() to do this within a script currently, but I have to manually shift the headerlines for each file as they change every time. I would like to do something like this:

DELIMITER = ',';
HEADERLINES = 50;  
data_imported = importdata(file_to_get, DELIMITER, HEADERLINES);

But the issue is that only 1 or 2 of my 300~ files has a headerline count of 50. Another very important note, is that the default number of headerlines for EVERY file only needs to have 2 added to it. So if the default headerline number while using uiimport() is 50, then i would need to change it to 52.

Would there be a way to change this with possibly an if statement? Note: defaultheader is being used as and example and not an actual variable.

for example:

if defaultheader = 30
     header = 32
end

example of what the .CSV file looks like in excel

*Title
*
*Description (1.7)   Tue 03/20/12 09:59:09
*
* file name
*
* TestNames:
* _Test 1   (ein: 10)
* _Test 2   (ein: 15)
* _Test 3   (ein: 20)
* _Test 4   (ein: 25)
* _test 5   (ein: 30)
* _test 6   (ein: 35)
* _test 7   (ein: 40)
* _test 8   (ein: 45)
* _test 9   (ein: 50)
* _test 10   (ein: 55)
* _test 11   (ein: 60)
* _test 12   (ein: 65)
* _test 13   (ein: 70)
* _test 14  (ein: 75)
* _test 15   (ein: 80)
* _test 16   (ein: 85)
*
* Info1: ...
* Info2: ...
* info3: ...
* info4: ...
* info5: ...
* info6: ...
* info7: ...
* info8: ...
* info9: ...
* info10: ...
* info11: ...
* info12: ...
*
* Pixel,Tap Name,Tap Pixel,brightness
,,,10,15,20,25,30,35,40
,,,0,67,21,12,223,231,832
1,0A1,458,75,89,24,46,256,763,532
2,0A1,457,43,65,56,554,263,254,732
3,0A1,456,28,47,76,221,412,732,832
4,0A1,455,12,23,36,466,652,633,637
5,0A1,454,11,78,98,678,864,241,223

Upvotes: 0

Views: 2137

Answers (1)

Amro
Amro

Reputation: 124563

You could use TEXTSCAN function to read the CSV files, and specify the CommentStyle option to ignore all header lines starting with a * character.

The code below will first read the file as a cell array of lines, ignoring all lines starting with *. We also skip the first two lines immediately after the header section. Next, we loop over each line, and parse the tab-delimited values as string. We ignore the first three columns, and convert the remaining values to numeric. Finally we store that row in the matrix M. Note that the code does not assume we know the number of columns in advance, and instead determine that from one of the data rows.

%# read lines
fid = fopen('file.csv','rt');
C = textscan(fid, '%s', ...
    'Delimiter','', 'Whitespace','\n', 'CommentStyle','*');
fclose(fid);
C = C{1}(3:end);    %# skip two lines

%# parse each line
numCols = sum(C{1}==',')+1 - 3;    %# number of columns
M = zeros(numel(C),numCols);
for i=1:numel(C)
    v = textscan(C{i}, '%s', 'Delimiter',',');
    v = str2double(v{1}(4:end));
    M(i,1:numel(v)) = v;
end

The resulting matrix M containing the part of the file you are interested in:

M =
    75    89    24    46   256   763   532
    43    65    56   554   263   254   732
    28    47    76   221   412   732   832
    12    23    36   466   652   633   637
    11    78    98   678   864   241   223

Upvotes: 1

Related Questions