Opening folders and read its information

I'm new to the field of creating or opening folders from a script. I have tried many things and I always get "the file was not found".
This is my main director (C:\Deterministic\New Objectives) where 8 sub-folders are located and their names are:
Normal_4000_M
Normal_4000_M_2S
Normal_4000_M_S
Normal_4000_M+2S
Normal_4000_M+S
Normal_4000_P10
Normal_4000_P50
Normal_4000_p90
I want to access each folder and load its 'NPV_v_0.15.mat' and save it in a vector which is called RESULT.
Anyone can help please and thanks in advanced.

2 Commenti

"where 8 sub-folders are located and their names are: 1)folder_4000_M 2)folder_4000_M_2S 3)fodler_4000_M+2S"
Do you have eight folders (as you tell us) or three (as you show us)?
fodler_4000_M+2S
^^
Is this spelling mistake included in the folder names?
Yeah did not include all the file names and did not write all the folder names as well. This is just an example and I will try it on mine

Accedi per commentare.

 Risposta accettata

Stephen23
Stephen23 il 4 Ott 2018
Modificato: Stephen23 il 4 Ott 2018
This should get you started:
D = 'C:\Deterministic\New Objectives';
S = dir(fullfile(D,'Normal_4000*'));
N = {S([S.isdir]).name};
C = cell(1,numel(N));
for k = 1:numel(N)
C{k} = load(fullfile(D,N{k},'NPV_v_0.15.mat'));
end
Your data will be in the cell array C. The folder names are listed in cell array N. If every .mat contains the same variables then you could easily concatenate them into one non-scalar structure:
T = [C{:}];
Read more:

4 Commenti

Yaser Khojah
Yaser Khojah il 4 Ott 2018
Modificato: Yaser Khojah il 4 Ott 2018
These are the subfolders as I have them: Normal_4000_M Normal_4000_M_2S Normal_4000_M_S Normal_4000_M+2S Normal_4000_M+S Normal_4000_P10 Normal_4000_P50 Normal_4000_p90 The script is running without any error but I do not see any data in C
@Yaser Khojah: your original question showed an incorrect folder name prefix (you used "folder"), which you have now changed to "Normal". I changed my answer to reflect your updated question. Try it again. The first thing you should do after running it is check the size of C:
size(C)
If you have any problems tell me what that command outputs.
Dear Stephen thank you so much. It is working finally. You made my day thanks a lot :)
Is there away I can run script in each of these folders. I want to run TESTING_4000.m please. I have tried many things but non worked for me. Can you please help. Thanks Stephen

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Environment and Settings in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by