MATLAB: How to extract a portion of a CSV file using MATLAB

MATLAB

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

Best Answer

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');