How to access data set from different folders?

27 ビュー (過去 30 日間)
SM
SM 2020 年 7 月 20 日
コメント済み: SM 2020 年 7 月 20 日
I can easily access .dat files from the current directory by using the following codes:
%% Input data set
load ProcMacMatrix.dat % load the file
load SuppMacMatrix.dat % load the file
load CostMatrix.dat % load the file
load Dataset.dat % load the file
load ProcessingTime.dat % load the file
My problem needs to access the similar data set, as described, from the Instance1 and Instance2. First, I want to access from the Instance1 and then Instance2. We may use for loop to access these two folders subsequently.
How can i do that?
I appreciate your help.

採用された回答

Walter Roberson
Walter Roberson 2020 年 7 月 20 日
finfo = dir('Instance*');
finfo(~[finof.isfolder]) = [];
foldernames = {finfo.name};
ninst = length(foldernames);
instance_data(ninst) = struct();
for K = 1 : ninst
thisfolder = foldernames{K};
instance_data(K).ProcMacMatrix = load(fullfile(thisfolder, 'ProcMacMatrix.dat')); % load the file
%and similar for the other files
end
  1 件のコメント
SM
SM 2020 年 7 月 20 日
Perfect! Thank you very much!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 7 月 20 日
Get a list of all files under the current folder like this:
filePattern = fullfile(pwd, '**/*.mat');
fileList = dir(filePattern);
for k = 1 : length(fileList)
thisFullfileName = fullfile(fileList(k).folder, fileList(k).name);
fprintf('Processing %s\n', thisFullfileName);
s = load(thisFullfileName)
end
  1 件のコメント
SM
SM 2020 年 7 月 20 日
Thank you!

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

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by