My matlab can no longer import csv with struct by using dir
3 ビュー (過去 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
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
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
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 Center および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!