Clear Filters
Clear Filters

My matlab can no longer import csv with struct by using dir

2 views (last 30 days)
I have to import 131 csv files for data analysis with matlab (version: Matlab 2022a). When i used the Matlab 2022a last Friday I have no problem to import it with dir function at all.
Here is the code that worked fine, until today:
dir ='C:\Users\User\Documents\PhD\2022winter\2_Stat_Downscaling\a2_b_HUC12_test\EA_LSTM\rrwhuc12\result\';
dir0 = [dir '0'];
S0 = dir(fullfile(dir0,'*.csv')); % create the struct file (133 x 1)
Table0 = zeros(2556,numel(S0),2); %create a table that I can save data from csv.
for K0 = 1:numel(S0)
F0 = fullfile(dir0,S0(K0).name);
T0 = readtable(F0);
Table0(:,K0,1)=table2array(T0(:,2));
Table0(:,K0,2)=table2array(T0(:,3));
end
However, after today, I found out that the identical code will give me this result after Line 3:
"Index exceeds the number of array elements. Index must not exceed 100."
In order to double-check, I tried the codes that worked in my past project and it gave me the same result. For unknown reasons, it now enforces Index to not exceed 100.
Why does that happen?
  3 Comments
Stephen23
Stephen23 on 4 Apr 2022
Edited: Stephen23 on 4 Apr 2022
"Here is the code that worked fine, until today: "
I doubt that.
As Fangjun Jiang correctly noted, you have shadowed the DIR function on the very first line of your code:
dir ='C:\Users\User\Documents\PhD\2022winter\2_Stat_Downscaling\a2_b_HUC12_test\EA_LSTM\rrwhuc12\result\';
^^^ This is the start of your problems.
dir0 = [dir '0'];
S0 = dir(fullfile(dir0,'*.csv'));
^^^ because now you cannot call DIR function here

Sign in to comment.

Answers (1)

Fangjun Jiang
Fangjun Jiang on 4 Apr 2022
dir() is a function yet you use "dir" as a variable name on the first line. Use a different name.
  1 Comment
Jan
Jan on 4 Apr 2022
In other words: Replace
dir ='C:\Users\User\Documents\PhD\2022winter\2_Stat_Downscaling\a2_b_HUC12_test\EA_LSTM\rrwhuc12\result\';
dir0 = [dir '0'];
S0 = dir(fullfile(dir0,'*.csv'));
by
folder = ['C:\Users\User\Documents\PhD\2022winter\2_Stat_Downscaling\', ...
'a2_b_HUC12_test\EA_LSTM\rrwhuc12\result\'];
folder0 = fullfile(folder, '0');
S0 = dir(fullfile(folder0, '*.csv'));

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!