Hi,
I need to upload files from two different folders. I wrote this but it woking just from one folder. How can I add the second folder?
dirname=['D:\Shared\MATLAB\100min\BT']
cd(dirname)
filelist = dir([basename '*_BTlength.mat'])
Thanks

 채택된 답변

Stephen23
Stephen23 2023년 2월 24일
편집: Stephen23 2023년 2월 24일

0 개 추천

Do NOT use ADDPATH or CD or any other messing around with the MATLAB search path just for acessing data files.
All MATLAB functions which import/export data files accept relative/absolute filenames. Using relative/absolute filenames is more robust, more efficient, and easier to debug than messing around with CD or the search path. You should use relative/absolute filenames.
For example:
P = 'D:\Shared\MATLAB\100min\BT'; % got rid of the superfluous square brackets.
S = dir(fullfile(P,'*_BTlength.mat'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
% do whatever you want with absolute filename F, e.g.:
D = load(F)
end
See also:

댓글 수: 3

Paola
Paola 2023년 2월 24일
Thanks but it still taking the files from one folder
P = 'D:\Shared\MATLAB\100min\BT
I would like to add another folder
P2 = 'D:\Shared\MATLAB\100min-rep2\BT
Stephen23
Stephen23 2023년 2월 25일
편집: Stephen23 2023년 2월 25일
"I would like to add another folder"
It is easy to specify two paths, e.g.:
P1 = 'D:\Shared\MATLAB\100min\BT';
P2 = 'D:\Shared\MATLAB\100min-rep2\BT';
S = [...
dir(fullfile(P1,'*_BTlength.mat'));...
dir(fullfile(P2,'*_BTlength.mat'))];
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
% do whatever you want with absolute filename F, e.g.:
D = load(F)
end
Alternatively you can use wildcards in the path name as well, e.g.:
P = 'D:\Shared\MATLAB\100min*\BT';
S = dir(fullfile(P,'*_BTlength.mat'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
% do whatever you want with absolute filename F, e.g.:
D = load(F)
end
Paola
Paola 2023년 3월 1일
Thanks, I was not able to find the error. it works now

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jacob Ward
Jacob Ward 2023년 2월 24일

0 개 추천

Use the addpath() function to add the path to your second folder to the working directory:

카테고리

도움말 센터File Exchange에서 Search Path에 대해 자세히 알아보기

질문:

2023년 2월 24일

댓글:

2023년 3월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by