|
a |
|
b/Semantic Features/parseCSV.m |
|
|
1 |
function [ valueArray headerArray dcmArray ] = parseCSV( fileName ) |
|
|
2 |
%parseCSV Returns a nice numerical matrix with all the values and a cell |
|
|
3 |
%array with all the headers |
|
|
4 |
% Note this is customized to expect the last cell to hold an image name |
|
|
5 |
% and automatically chops off the extension to turn it into an integer |
|
|
6 |
|
|
|
7 |
textData = importdata(fileName); |
|
|
8 |
headerText = textData{1}; |
|
|
9 |
|
|
|
10 |
%Get array of all the header's |
|
|
11 |
[value, remainder] = strtok(headerText, ','); |
|
|
12 |
headerArray = {value}; |
|
|
13 |
while length(remainder) > 0 |
|
|
14 |
%fprintf('%s\n', value); |
|
|
15 |
[value, remainder] = strtok(remainder, ','); |
|
|
16 |
headerArray{length(headerArray) + 1} = value; |
|
|
17 |
|
|
|
18 |
end |
|
|
19 |
%fprintf('%s\n', value); |
|
|
20 |
|
|
|
21 |
%Get the data |
|
|
22 |
valueArray = zeros(length(textData)-1, length(headerArray)); |
|
|
23 |
for i = 2:length(textData); %for each row in the file |
|
|
24 |
[value, remainder] = strtok(textData{i}, ','); |
|
|
25 |
valueArray(i-1, 1) = str2num(value); |
|
|
26 |
for j = 2:length(headerArray)-1 %last one is filename |
|
|
27 |
%fprintf('%s\n', value); |
|
|
28 |
[value, remainder] = strtok(remainder, ','); |
|
|
29 |
valueArray(i-1, j) = str2num(value); |
|
|
30 |
|
|
|
31 |
end %Special case to handle the file name ID at the end |
|
|
32 |
[value, remainder] = strtok(remainder, ','); |
|
|
33 |
value = value(1:end-4); |
|
|
34 |
%fprintf('%s\n', value); |
|
|
35 |
valueArray(i-1, j+1) = str2num(value); |
|
|
36 |
end |
|
|
37 |
|
|
|
38 |
dcmArray = valueArray(:,end); |
|
|
39 |
headerArray = headerArray(:,1:end-1); |
|
|
40 |
valueArray = valueArray(:,1:end-1); |
|
|
41 |
|
|
|
42 |
clear textData headerText |