Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
Pai-Feng Teng
Pai-Feng Teng le 4 Avr 2022
Modifié(e) : Stephen23 le 4 Avr 2022
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 commentaires
Pai-Feng Teng
Pai-Feng Teng le 4 Avr 2022
Line 3 generated the error. Here is the full message.
Stephen23
Stephen23 le 4 Avr 2022
Modifié(e) : Stephen23 le 4 Avr 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

Connectez-vous pour commenter.

Réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by