How can I access data from folders?

Code
files = dir('H:\Project Two\Programming and DataSet');
% Get a list of all files and folders in this folder.
names = {files.name};
% Get a logical vector that tells which is a directory.
dirFlags = [files.isdir] & ~strcmp(names, '.') & ~strcmp(names, '..');
% Extract only those that are directories.
subDirsNames = names(dirFlags);
Output
subDirsNames =
1×2 cell array
{'Instance01'} {'Instance02'}
Now, Instance01 folder has two files which are costMatrix.dat and ProcessMatrix.dat. Silmilarly, Instance02 folder has two files which are costMatrix.dat and ProcessMatrix.dat. First, I want to access and work on Instance01 folder and once it is done, I want to access and work on the Instance02. How can I do that?

Answers (1)

files = dir('H:\Project Two\Programming and DataSet\*\*.dat');
names = fullfile( {files.folder}, {files.name} );
names will now be the fully-qualified names of .dat files, each with its appropriate folder name.
Note though that names will not be subdivided up into folders.

4 Comments

SM
SM on 13 Jul 2020
Edited: SM on 13 Jul 2020
Thank you for your answer! but i want something else.
%% Data access
Instance = dir('H:\Project Two\Programming and DataSet\Instance01\*.dat');
N = length(Instance) ;
% loop for each file
for i = 1:N
DataSet = Instance(i).name ;
A{i,1}=dlmread(DataSet);
end
## In my problem, I have two folders such as Instance01 (H:\Project Two\Programming and DataSet\Instance01) and Instance02 (H:\Project Two\Programming and DataSet\Instance02). First i want to access and work on the data set contained in Instance01 as examplified in above. Once some operations will be completed, I start working on folder Instance02, accessing all .dat files. It could be in a loop. How can I do that?
What are the circumstances under which you switch from working on the first folder to working on the second folder? Do you switch back and forth between the two folders?
It appears to be important to you that Instance01 is processed before Instance02, but it also appears that you cannot or else do not want to hardcode the folder names -- so for example you might have a folder with Instance07 and Instance17 and Instance23 . How can you determine from the folder names which folders need to be processed first and in which order, and the point at which to switch to the second folder? And possibly on to the third as well?
Instance01 and Instance02 contain similar types of data set but in different sizes. First I want to work on the data set of Instance01 to be optimized and store in another file. Once the Instance01 will be optimized, the Instance02 need to be accessed and optimized. So one folder will be accessed once.
Folder name could be sequential such as Instance1, Instance2, Instance3,.....,Instance20. So, sprintf('Instance%d', k) could be used. It forces to come sequentially I guess. What do you think?
sprintf('Instance%d', k)
should work if you want folders such as Instance1 Instance2 . Your earlier used Instance01 Instance02 which would correspond to
sprintf('Instance%02d', k)

Sign in to comment.

Categories

Asked:

SM
on 13 Jul 2020

Commented:

on 17 Jul 2020

Community Treasure Hunt

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

Start Hunting!