Reading through folders getting the warning : ." Index in position 1 exceeds array bounds (must not exceed 1).".

2 ビュー (過去 30 日間)
Hello
The answer might be failry easy, but i am kind of stuck in it.
I'm trying to run through multiple subforlders to get a plot which contains data from all these folder. The goal is to compare the results.
But I get the Warning: " Index in position 1 exceeds array bounds (must not exceed 1). " in the second step when reading the files.
D_rough = 'C:\Users\ammar\OneDrive\Dokumenter\Master 2019\waves\Analysis\DATA_WAVE BASIN\Rough';
directories = dir(D_rough,'dir*');
12.JPG
I hope you can help :)
  2 件のコメント
Stephen23
Stephen23 2019 年 9 月 14 日
編集済み: Stephen23 2019 年 9 月 14 日
I would be very surprised if your code works without error, because you call dir with two input arguments:
dir(D_rough,'dir*')
whereas its documentation clearly only shows one input argument is supported. You need something like this:
dir(fullfile(D_rough,'dir*'))
"I get the Warning: " Index in position 1 exceeds array bounds (must not exceed 1). "
That is an error, not a warning.
Ammar Alfaili
Ammar Alfaili 2019 年 9 月 14 日
To be honest I have tryied that one, but then I got This warning:
12.JPG
12.JPG

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2019 年 9 月 14 日
編集済み: the cyclist 2019 年 9 月 14 日
At some point, did you define a variable named dir? MATLAB would then try to access that variable, rather than the command. Clear that variable, and see if it works.
  3 件のコメント
the cyclist
the cyclist 2019 年 9 月 14 日
The lesson here: Never use MATLAB keywords as variable names. :-)
Ammar Alfaili
Ammar Alfaili 2019 年 9 月 14 日
Yes, Thanx a lot
Have a nice day :)

サインインしてコメントする。

その他の回答 (1 件)

Ammar Alfaili
Ammar Alfaili 2019 年 9 月 14 日
編集済み: Ammar Alfaili 2019 年 9 月 14 日
I thought this was going to be easy now there's a new problem.
After runnig the code, the plot doesn't appears. There is no warning though.
D_rough = 'C:\Users\ammar\OneDrive\Dokumenter\Master 2019\waves\Analysis\DATA_WAVE BASIN\Rough';
S = dir(fullfile(D_rough,'dir*'));
N = setdiff({S([S.isdir]).name},{'dir0','dir1','dir2','dir3'}); % list of subfolders of D_rough.
for ii = 1:numel(N)
T1 = dir(fullfile(D_rough,N{ii},'dir*')); % improve by specifying the file extension.
C = {T1(~[T1.isdir]).name}; % files in subfolder.
for jj = 1:numel(C)
F = fullfile(D_rough,N{ii},C{jj});
figure()
plot(tA,Fx)
% hold on
% plot(t_top3,max(Fx_chosen),'x','MarkerSize',20)
% legend('')
ylabel('\it Horizontal Force, F_x \rm [N]')
xlabel('\it Time \rm [s]')
grid
title('filename')
legend boxoff
linkaxes(s,'x')
set(gcf,'position',[381 678 1210 420]);
xlim([t_top3-0.5*T t_top3+0.5*T]);
% cd('..')
end
end
I really don't know what's wrong this time .
I hope you can help
  3 件のコメント
Stephen23
Stephen23 2019 年 9 月 15 日
編集済み: Stephen23 2019 年 9 月 15 日
Your dir usage is very unusual. It is quite possible that none of it works as you expect.
For example:
S = dir(fullfile(D_rough,'dir*'));
N = setdiff({S([S.isdir]).name},{'dir0','dir1','dir2','dir3'});
You exclude the directories dir0, dir1, dir2 and dir3 from N. Is that really the intention? (In my original code that you copied this from, setdiff was used to remove . and ..)
T1 = dir(fullfile(D_rough,N{ii},'dir*')); % improve by specifying the file extension.
C = {T1(~[T1.isdir]).name}; % files in subfolder.
Are your files really named dir... ? That is highly unusual: you have subfolders named dir... and in them are files named dir..., without any file extensions. Possible, but very odd.
You even copied my code comment verbatim: did you follow my advice?
Ammar Alfaili
Ammar Alfaili 2019 年 9 月 16 日
Hi Stephen
I guess i misunderstood the procedures and still have some doubts about it. Let me work on it and see if I can make it work. I have been busy with some other analysis lately.
But thanx for your advice :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by