Error in for Loop to upload data from multiple CSV Files

9 次查看(过去 30 天)
Hello,
I am attempting to upload .csv files from a folder. I am getting an error message which I don't understand:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in ImportData (line 12)
filePattern = fullfile(myfolder, '*.txt');
I want to make a separate table from each .csv file uploaded. The .csv files contain two columns of useful data. I am trying to piece together what others have done before but I'm pretty new to this.
Eventually I will want to plot and fit a certain portion of the data contained within each file and make individual plots of each. I want to add this to this code eventually.
%% Import data from text file
myfolder = 'C:\Users\dvanhoom\OneDrive - Colorado School of Mines\Documents\MatLabUpload';
% Checks to see if folder is empty or not
if ~isfolder(myfolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', DATA);
uiwait(warndlg(errorMessage));
return;
end
% grabbing CSVs
filePattern = fullfile(myfolder, '*.txt');
file = dir(filePattern);
Q = numel(file);
table = cell(1, Q);
%Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 3);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["Time", "DeltaOD", "VarName3"];
opts.VariableTypes = ["double", "double", "string"];
% Specify file level properties
opts.MissingRule = "omitvar";
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, "VarName3", "WhitespaceRule", "preserve");
opts = setvaropts(opts, "VarName3", "EmptyFieldRule", "auto");
for k = 1 : Q
baseFileName = filedir(k).name; %calls file name from position k in firdir
fullFileName = fullfile(DATA, baseFileName); %concatenate file directory and baseFileName to give full absolute path
table{k} = readtable(fullFileName, opts); %imports data from csv
fprintf('read file %s\n', fullFileName); %show which file is being processed in command window
end

回答(1 个)

Walter Roberson
Walter Roberson 2019-11-19
You have accidentally created a variable named fullfile

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by