MATLAB Answers

Retrieve data from several folders and structures

3 ビュー (過去 30 日間)
Isma_gp
Isma_gp 2018 年 12 月 16 日
コメント済み: Isma_gp 2019 年 1 月 8 日
Hi,
I have data organized in folders. Each folder contains a structure. I need to go through all the folders retrieving some of the fields from the structures, and organize the data in a new structure.
The folders are folder_1, folder_2.....folder_n. Each folder contains a structure called motion_data. I need to extract the information from motion_data.response.X, which is a 20x15 double.
I would like to structure the extracted data in the following manner:
Newstructure.folder_1.responseX
Newstructure.folder_2.responseX
etc.
Can I get help with this? I'm not sure waht is an efficient way to go through all the folders and retrieve the info to save it in the new structure.
Thanks!

採用された回答

GT
GT 2018 年 12 月 16 日
If I understand correctly you have data files in different folders and you want to take subset of that data and reorganize it in a new structure.
To solve this there are a couple of things you can try: (I am on a mac and R2018b so the slashes might be different for you).
a = dir('**/*.mat'); % I am assuming the data you have in each folder is a type *.mat but if it is something else change this
newStruct = []
for i = 1:size(a,1)
% Read data from each file in which ever way you do today
mydata = load(a(i).File)
[~,d,~] = fileparts(a(i).folder)
newStruct = setfield(newStruct,d,mydata)
end
Please note that this will kind of work, depends on how you store your data in each of the folders but the key things here are:
  • fileparts gets information from dir and parses the folders
  • setfield is how you programatically add fields to structures
Hope this helps you

  3 件のコメント

Isma_gp
Isma_gp 2019 年 1 月 7 日
Hi, this was very helpful and I've been using it.
I have now to retrieve data from a structure in a similar fashion, however, the problem is slightly different. Do you think you could help me with a similar solution?
The data is still organized in folders.
The folders are folder_1, folder_2.....folder_n. Each folder contains a structure called moo_data. Each structure contains several structures (file_1, file_2, file_3...etc). Each of these structures contains a double (24x20) called response that I need to extract.
I would like to structure the extracted data in the following manner:
Newstructure.folder_1.response
Newstructure.folder_2.response
GT
GT 2019 年 1 月 8 日
When you say each folder contains moo_data, is that a mat file in each folder that when you load it, you get a structure moo_data? This might be a good candidate for a new Answer question:)

Sign in to comment.

その他の回答 (0 件)


Translated by