필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Pai-Feng Teng
Pai-Feng Teng 2022년 4월 4일
편집: Stephen23 2022년 4월 4일
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
Pai-Feng Teng
Pai-Feng Teng 2022년 4월 4일
Line 3 generated the error. Here is the full message.
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