Acces directories of specific name

조회 수: 1 (최근 30일)
ARP
ARP 2018년 7월 27일
댓글: Stephen23 2018년 7월 27일
Hi all,
I have a question on how to access specific sub-folders by name.
Lets say I have a working folder where there are sub-folders called:
-power
-thickness
I am using a GUI in which I use the following code to access the individual sub-folder of interest.
folder = uigetdir('C:\Users\...\working folder');
files = dir(fullfile(folder, '*.txt'));
After choosing the sub-folder manually with uigetdir, the procedure works well. I would like to do all simply by indicating where is the working folder with one push-button.
I am guessing that should be something like
folder_destination1 = folder,'power';
files1 = dir(fullfile(folder_destination1, '*.txt'));
folder_destination1 = folder,'power'; files2 = dir(fullfile(folder_destination2, '*.txt'));
How to update the folder name by updating with the sub-folder name?
Thanks in advance
  댓글 수: 2
ARP
ARP 2018년 7월 27일
I found the answer. Cheers
folder = uigetdir('C:\Users\...\working folder');
folder_sub1 = fullfile(folder,'power');
folder_sub2 = fullfile(folder,'thickness');
files1 = dir(fullfile(folder_sub1, '*.txt'));
files2= dir(fullfile(folder_sub2, '*.txt'));
Stephen23
Stephen23 2018년 7월 27일
@ARP: simpler:
workpath = uigetdir('C:\Users\...\working folder');
files1 = dir(fullfile(workpath,'power','*.txt'));
files2 = dir(fullfile(workpath,'thickness','*.txt'));

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

채택된 답변

Stephen23
Stephen23 2018년 7월 27일
편집: Stephen23 2018년 7월 27일
You can get the path of the working folder by using fileparts:
[workpath,subfolder] = fileparts(folder)
Once you have the working path then you generate the subfolder path easily:
fullfile(workpath,'power','*.txt')
fullfile(workpath,'thickness','*.txt')
  댓글 수: 2
ARP
ARP 2018년 7월 27일
Thanks but is not working. The idea is to select the working folder with uigetdir.
There should be instructions to open automatically the sub-folder called "power" and "thickness".
It would continue as:
NF1 = length(files1);
for i = 1:NF1
B1=import_AB_function(fullfile(folder1,files1(i).name));
t1{i} = B1;
Time_R1 = (t{:, i}(:,1));
Ampl_R1 = (t{:, i}(:,end));
end
NF2 = length(files2);
for i = 1:NF2
B2=import_AB_function(fullfile(folder2,files2(i).name));
t2{i} = B2;
Time_R2 = (t{:, i}(:,1));
Ampl_R2 = (t{:, i}(:,end));
end
Stephen23
Stephen23 2018년 7월 27일
편집: Stephen23 2018년 7월 27일
"The idea is to select the working folder with uigetdir."
Sure, like this:
workpath = uigetdir(...);
"There should be instructions to open automatically the sub-folder called "power" and "thickness"."
I don't know what you mean by "open" a folder: MATLAB is not an application that "opens" folders. But if you mean that you want to read/write files from those folders, then the code I gave in my answer will help you:
S = dir(fullfile(workpath,'power','*.txt'))
N = numel(S);
C = cell(1,N);
for k = 1:N
F = fullfile(workpath,'power',S(k).name);
C{k} = load_your_data(F);
end
and the same for 'thickness'. Note that fullfile accepts more than two input arguments.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Name Construction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by