Error in for Loop to upload data from multiple CSV Files

1 回表示 (過去 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 Center および File ExchangeText Files についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by