How to load *.mat file from each directories in new script?
Mostra commenti meno recenti
I have 15 directories and in each directories, I have minimum 8 sub-directories( varies in each directories). For each directory, I have .mat file that I have saved for future use. Now, I want to develop a new script which load .mat file in each directory? How do I load .mat file from each directory? The name of .mat file is same for each directory. Any idea is highly appreciated. Thank you
3 Commenti
Andrew Newell
il 20 Apr 2017
Modificato: Andrew Newell
il 20 Apr 2017
Depends what you want to do with each .mat file. Do you process its contents and then overwrite it with the next .mat file? Do you have a .mat file in each sub-directory? And do you know in advance what the directories are, or will you need to determine that automatically?
sam moor
il 21 Apr 2017
Andrew Newell
il 21 Apr 2017
Fortunately, the code I provided below is compatible with that.
Risposte (2)
Image Analyst
il 22 Apr 2017
1 voto
See attached demo. If you have R2016b or later, use that one, which has enhanced capabilities for dir() that mean you don't need to call genpath() anymore.
2 Commenti
Andrew Newell
il 22 Apr 2017
Fancy! Is this in the FEX?
Image Analyst
il 22 Apr 2017
No. Currently I have about 250 demo scripts. I just put the most "important" ones in my File Exchange. I'm afraid if I put up every little demo that the important ones might get lost in the larger group of them. Every once in a while I think of zipping them all up into something like a "grab bag of Image Processing Demos" and upload that, but I haven't yet.
Andrew Newell
il 20 Apr 2017
Here is a solution that assumes there is one of these .mat files in every subdirectory of each of the 15 directories. Let's also assume that all 15 directories are all in the same directory, and fullrootdirectoryname is its name. Also, there are no other directories in that space. Then you can do the following to visit each directory and load the file:
C = strsplit(genpath(fullrootdirectoryname),':'); % Generate a list of folders
startingFolder = pwd;
for ii=1:length(C)
cd(C{ii})
load yourFilename.mat
end
cd(startingFolder)
The business with startingFolder makes sure you end up back where you started, and not in some unexpected place. If any of the above assumptions are not met, this code will need to be modified.
13 Commenti
Andrew Newell
il 21 Apr 2017
Modificato: Andrew Newell
il 21 Apr 2017
A thought just occurred to me - are you on a PC? If so, I'll need to see what the output looks like when you run
genpath(fullrootdirectoryname)
I don't have a PC, so I'll have to do some reverse engineering.
Andrew Newell
il 21 Apr 2017
Try replacing the top line in my code by the following:
pp = genpath(fullrootdirectoryname);
searchstr = [':(?=.:',filesep,')'];
C = regexp(pp,searchstr,'split');
sam moor
il 21 Apr 2017
Andrew Newell
il 21 Apr 2017
O.k., what platform are you running MATLAB on? And can you show me what the result of genpath is?
Andrew Newell
il 21 Apr 2017
Fine, but that isn't the information I need.
Walter Roberson
il 21 Apr 2017
The use of "File folder" suggests MS Windows to me.
Andrew Newell
il 21 Apr 2017
As does the error "Cannot CD to E". But I don't know what the output of genpath looks like on Windows. I thought I could guess, but apparently not.
sam moor
il 21 Apr 2017
sam moor
il 21 Apr 2017
Andrew Newell
il 22 Apr 2017
- Which version of MATLAB do you have?
- Try running
which genpath -all
and please show me what you get.
sam moor
il 23 Apr 2017
Andrew Newell
il 23 Apr 2017
That also answers the question about the MATLAB version. So its output should be a string. Can you please show me the string?
Categorie
Scopri di più su Search Path in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!