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

2 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2022-4-4
编辑:Stephen23 2022-4-4
"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

请先登录,再进行评论。

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2022-4-4
dir() is a function yet you use "dir" as a variable name on the first line. Use a different name.
  1 个评论
Jan
Jan 2022-4-4
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'));

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by