How can I extract a portion of a CSV file using MATLAB?
24 views (last 30 days)
Show older comments
I have a CSV file with the following format:
Header 1;Header 2;Header 3;Header 4
1;2;3;4
5;6;7;8
9;10;11;12
13;14;15;16
I would like to extract the header and part of the data, for example as follows:
Header 2;Header 3
6;7
10;11
Accepted Answer
MathWorks Support Team
on 2 Feb 2010
The following code demonstrates how this functionality can be achieved:
function test
% Set parameters.
inputFile = 'input.csv';
outputFile = 'output.csv';
totalColumns = 4;
rowBegin = 2;
rowEnd = 3;
columnBegin = 2;
columnEnd = 3;
delimiter = ';';
% Call function.
extractdata(inputFile, outputFile, totalColumns, rowBegin, rowEnd, columnBegin, columnEnd, delimiter);
% Function definition.
function extractdata(inputFile, outputFile, totalColumns, rowBegin, rowEnd, columnBegin, columnEnd, delimiter)
% Open input file.
fid = fopen(inputFile, 'r');
% Get header in one line and extract .
headerLines = fgetl(fid);
% Remove trailing delimiter.
if strcmp(headerLines(end), delimiter)
headerLines = headerLines(1:end-1);
end
% Extract requested portion of the header.
headerLines = textscan(headerLines, [repmat('%*s', 1, columnBegin - 1) repmat('%s', 1, columnEnd - columnBegin + 1) repmat('%*s', 1, totalColumns - columnEnd)], 'Delimiter', delimiter, 'CollectOutput', 1);
headerLines = headerLines{1,1};
% Get integer (%d) data, extracting the requested portion.
% web([docroot '/techdoc/ref/textscan.html'])
C = textscan(fid, [repmat('%*d', 1, columnBegin - 1) repmat('%d', 1, columnEnd - columnBegin + 1) repmat('%*d', 1, totalColumns - columnEnd)], rowEnd, 'Delimiter', delimiter, 'CollectOutput', 1);
% Close input file.
fclose(fid);
% Extract rows.
C = C{1,1};
C = C(rowBegin:rowEnd,:);
% Write extracted column headers in new file.
headerLines = cellfun(@(x)sprintf(['%s' delimiter],x), headerLines, 'UniformOutput', false);
headerLines = cell2mat(headerLines);
headerLines = [headerLines(1:end-1) '\n']; % Remove trailing delimiter and append newline.
fid = fopen(outputFile, 'wt');
fprintf(fid, headerLines);
fclose(fid);
% Append numeric data.
dlmwrite(outputFile, C, '-append', 'delimiter', delimiter, 'precision', '%d', 'newline', 'pc');
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!