Error in for Loop to upload data from multiple CSV Files

조회 수: 9 (최근 30일)
Daniel Joseph Van Hoomissen
Daniel Joseph Van Hoomissen 2019년 11월 19일
댓글: Walter Roberson 2019년 11월 19일
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
  댓글 수: 2
Daniel Joseph Van Hoomissen
Daniel Joseph Van Hoomissen 2019년 11월 19일
DOH, Thanks, fixed that error.
But now I find that each table is inserted into a cell within an array.
How can I create an individual table from each .csv instead of having them in this 10000x3 table format in each cell?

댓글을 달려면 로그인하십시오.

카테고리

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