How to load *.mat file from each directories in new script?

1 回表示 (過去 30 日間)
sam moor
sam moor 2017 年 4 月 20 日
コメント済み: Andrew Newell 2017 年 4 月 23 日
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 件のコメント
sam moor
sam moor 2017 年 4 月 21 日
No I don't want to overwrite each .mat file rather load each .mat file from each directory and save each .mat file in matrix form
Andrew Newell
Andrew Newell 2017 年 4 月 21 日
Fortunately, the code I provided below is compatible with that.

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2017 年 4 月 22 日
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 件のコメント
Andrew Newell
Andrew Newell 2017 年 4 月 22 日
Fancy! Is this in the FEX?
Image Analyst
Image Analyst 2017 年 4 月 22 日
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
Andrew Newell 2017 年 4 月 20 日
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 件のコメント
sam moor
sam moor 2017 年 4 月 23 日
when I type which genpath -all it gives C:\Program Files\MATLAB\R2016a\toolbox\matlab\general\genpath.m
Andrew Newell
Andrew Newell 2017 年 4 月 23 日
That also answers the question about the MATLAB version. So its output should be a string. Can you please show me the string?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by