How to transfer folder structure to struct data?

I have big data inside some folder structure.
I need transfer almost whole(bold path) folder structure in my final struct file with data.
Question is: How to do it better than it is? I know I have 'dir' function but how to use it more smart in my case..
Structure like this:
C:\...\folders\release\mode\const\var\...\... .txt
folders=dir(path);
datas=struct;
s_fol=size(folders,1);
for ii=3:s_fol
cd(path);
if isfolder(folders(ii).name)
fol_now=folders(ii).name;
datas.(fol_now)=struct;
datas.(fol_now).Released=struct;
mode_folder=dir(strcat(path,{'\'},folders(ii).name,{'\Released'}));
s1_fol=size(mode_folder,1);
for ii1=3:s1_fol
cd(strcat(path,{'\'},folders(ii).name,{'\Released'}))
if isfolder(mode_folder(ii1).name)
mode_fol_now=mode_folder(ii1).name;
datas.(fol_now).Released.(mode_fol_now)=struct;
datas.(fol_now).Released.(mode_fol_now).dyn=struct;
var_fol=dir(strcat(path,{'\'},folders(ii).name,{'\Released\'},mode_fol_now,{'\dyn'}));
s2_fol=size(var_fol,1);
for ii2=3:s2_fol
cd(strcat(path,{'\'},folders(ii).name,{'\Released\'},mode_fol_now,{'\dyn'}))
if isfolder(var_fol(ii2).name)
var_fol_now=var_fol(ii2).name;
datas.(fol_now).Released.(mode_fol_now).dyn.(var_fol_now)=struct;
actual_name=strcat(path,{'\'},folders(ii).name,{'\Released\'},mode_fol_now,{'\dyn\'},var_fol_now);
cd(actual_name)
if exist('config.m', 'file') == 2
% PROCESSING DATA & STORE IN STRUCT
end
end
end
end
end
end
end

댓글 수: 5

Stephen23
Stephen23 2019년 5월 30일
편집: Stephen23 2019년 5월 30일
"How to do it better than it is?"
  • Do not use cd.
  • Use fullfile instead of string concatenation.
  • Use the isdir field rather than calling isfolder.
  • Avoid exist if you can, e.g. the files return by dir exist by definition of that function.
  • Avoid nested structures (they are tedious to access data in).
  • Avoid putting meta-data (e.g. folder names) into code (e.g. as fieldnames).
Note that with MATLAB >=R2016b you could probably do most of this with one dir call, using the new wildcard syntax. What MATLAB version are you using?
I use latest MATLAB version but need compability with R2018b.
I agree with you, but how it can be done with minimal dir calling?
Jan
Jan 2019년 5월 31일
The question is not clear. For which .txt files are you looking? Does the existance of a config.m file is the specified folder matter?
It doesn't matter, processing .txt inside core program.
existance config.m in defined folder -- condition for run core program.
I just intresting in creating folder structure like in windows
Jan
Jan 2019년 5월 31일
I do not understand the 3 sentences of this comment.
What is a "core program" and which ".txt" files are processed by what?
How can this information be used: "existance config.m in defined folder -- condition for run core program"?
In which windows? A folder structure can be found in the file system. Are you sure that it is useful to copy this structure to a nested struct?

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2019년 5월 31일

0 개 추천

It does not sound like a good design to use the folder names as field names. Keeping the names in a cell string might be smarter:
data(k).value = rand(10);
data(k).condition = {'folders', 'release', 'mode', 'const', 'var'};
Deeply nested structs are rarely useful for processing. But if you have a good reason to do so:
% Get all config.m files recursively (It is just a guess, that you need this):
BasePath = 'C:\Temp\';
FileList = dir(fullfile(BasePath, '**\config.m'));
data = struct([]);
for iFile = 1:numel(FileList)
Folder = FileList(iFile).path(numel(BasePath)+1:end);
Keys = strsplit(Folder, filesep);
data.(Keys{1}).(Keys{2}).(Keys{3}).(Keys{4}).(Keys{5}) = ???
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

릴리스

R2018b

태그

질문:

2019년 5월 30일

댓글:

2019년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by