forked from episodes-platform/shared-snippets
33 lines
1.2 KiB
Mathematica
33 lines
1.2 KiB
Mathematica
|
% -----------------
|
||
|
% Copyright © 2023 ACK Cyfronet AGH, Poland.
|
||
|
% -----------------
|
||
|
|
||
|
function data = readAndCheckHeaders(csvFilePath, column_desc, doCheckHeaders)
|
||
|
% DESCRIPTION: Program that reads content from the CSV file, checking if the content matches the headers defined
|
||
|
% in the column_desc structure. The returned value is a cell with all values from the csv file.
|
||
|
% INPUTS:
|
||
|
% - csvFilePath : path to the CSV file
|
||
|
% - column_desc : structure containing definition of the CSV columns and their mapping to the final object
|
||
|
|
||
|
fid = fopen(csvFilePath);
|
||
|
data = textscan(fid, '%q', 'Delimiter', ','){1}; % cell with all values from the csv file
|
||
|
fclose(fid);
|
||
|
if doCheckHeaders
|
||
|
check_headers(data, column_desc);
|
||
|
end
|
||
|
end
|
||
|
|
||
|
function check_headers(data, column_desc)
|
||
|
colCount = length(column_desc);
|
||
|
headers = data(1:colCount);
|
||
|
for i=1:colCount
|
||
|
if ~strcmp(column_desc(i).nameInCsv, headers(i))
|
||
|
error('Expected column %s, but found %s in CSV headers', column_desc(i).nameInCsv, char(headers(i)));
|
||
|
end
|
||
|
end
|
||
|
|
||
|
if mod(length(data), colCount) ~= 0
|
||
|
error('Improper number of values in one of the rows');
|
||
|
end
|
||
|
|
||
|
end
|