Error importing data from .CSV file

3 Ansichten (letzte 30 Tage)
John Andrew
John Andrew am 20 Feb. 2020
Bearbeitet: John Andrew am 20 Feb. 2020
Hello,
I currently have a matlab script, which calls a function that reads through a series of .csv files and pulls specified information [Below is code for function.m]. The matlab code will work with archived .csv files, but won't work with newly generated .csv files or recently updated/saved .csv files and am unsure why. See code below for function:
function [data] = readData(fileName)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
colNames = ''; pids = '';
fid = fopen(fileName);
readColNames = true;
readPIDs = false;
lineCount = 0;
W1flag = false;
dataComing = false;
while ~feof(fid)
currLine = strtrim(fgetl(fid));
lineCount = lineCount + 1;
% catch first line without W1 after detection of lines with W1
if ~startsWith(currLine,'W1') && W1flag
break
end
% catch first line containing W1
if startsWith(currLine,',W1')
W1flag = true;
end
% detects when mnemonics begin
if startsWith(currLine,',T')
readColNames = false;
readPIDs = true;
end
% assumes files starts with listing of column names (actual PIDs)
if readColNames
colNames = [colNames, currLine];
end
if readPIDs && ~W1flag
pids = [pids, currLine];
end
end
fclose(fid);
colNames = strrep(colNames,'/','_');
colNames = strsplit(colNames,',');
% remove duplicates
[cols, ind] = unique(colNames(2:end));
colNames = [colNames{1} colNames(ind+1)];
ind = [1 ind(:)'+1];
pids = strsplit(pids,',');
pids{1} = 'Date';
pids = pids(ind);
numHeaderLines = lineCount - 1;
lineCount = 0;
tic
tempDataFile = 'data_temp.txt';
fid_temp = fopen(tempDataFile,'w+');
fid = fopen(fileName,'r');
while ~feof(fid)
currLine = strtrim(fgetl(fid));
lineCount = lineCount + 1;
if (lineCount > numHeaderLines)
currLine = regexprep(currLine,'(\d[+-])(\d)','$1e$2');
fprintf(fid_temp,[currLine,'\n']);
end
end
fclose(fid);
fclose(fid_temp);
toc
% clear formatSpec
% formatSpec = '%D';
% for k = 1:numel(pids)-1
% formatSpec = [formatSpec ' %g'];
% end
% fid = fopen(fileName);
% data = textscan(fid,formatSpec,'Delimiter',',','HeaderLines',lineCount-1);
% fclose(fid);
opts = detectImportOptions(tempDataFile);
% data = readtable(fileName,'HeaderLines',lineCount-1,'ExpChars',' ','Delimiter',',');
data = readtable(tempDataFile,opts);
data = data(:,ind);
data.Properties.VariableNames = colNames;
data.Properties.VariableDescriptions = pids;
data.Date_Time = datetime(data.Date_Time,'InputFormat','MM/dd/yyyy HH:mm:ss.SSS');
data(1:10,:)
% data.Properties.Description = pids;
% mid = floor(size(data,1)/2);
% data(mid-10:mid+10,:)
delete(tempDataFile)
end
The error I get, is as follows:
Index exceeds the number of array elements (7).
Error in readData (line 52)
pids = pids(ind);
Error in Procedure_1 (line 29)
data = readData(currFile);
For instance, the code will break if I take a .csv file that originally worked with the code and then... file > save > close.. resulting in the matlab code breaking. I'm very confused how this can be.
Note: The archived .csv files I'm working with are only from 2019, so they're still new.
Thanks for the help!

Antworten (0)

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by