Matlab tool to extract data from struct to seperate .mat files
Show older comments
Hello together,
i want to build a tool (best case a GUI) where you can load in data (.mat struct files) and extract the different values to seperate .mat files which then can be saved as new .mat files.
I don't know the names of the variables in the struct.
Would be very nice to get some help from you guys, thanks!
8 Comments
Walter Roberson
on 28 Dec 2022
Does each mat file have a single variable that is a nonscalar struct array that is to be split? Does each one have a number of variables (of various names) and the task is to create one mat for each variable?
Henning Schmitz
on 30 Dec 2022
Walter Roberson
on 30 Dec 2022
Okay, how should the output files be named? I assume that you cannot promise that each variable name is unique over all of the files.
Jan
on 30 Dec 2022
What exactly is the problem? You want to create a GUI. Then decide, if it should be a figure or uifigure, if you want to use GUIDE or AppDesigner or if you create it programmatically. The readers cannot create a GUI for you, because the details are not clarified yet.
The names of the fields of the struct are not known in advance, but loading the structs is easy and the command fieldnames provide a list of fieldnames.
I suggest to start to create the GUI at first step by step. Then explain explicitly, which problem occurs.
Rahul
on 2 Jan 2023
Please elaborate the question clearly. I will create a GUI for you. Below are my concerns:
- Does the MAT file contain only the struct data-types or any other numeric datatypes as well?
- How do you want to separate the data and then export it to the other MAT files?
- It would be better if you share some generic .MAT files and mention expected output MAT files and the which variable to include in that partcular MAT file.
Henning Schmitz
on 2 Jan 2023
As Jan wrote, LOAD the MAT-file and loops over its content using FIELDNAMES():
S = load('rec1_002.mat')
A = struct2cell(S);
B = fieldnames(S);
for k = 1:numel(A)
T = cell2struct(A(k),B(k));
F = sprintf('%s.mat',B{k});
save(F,'-struct','T')
end
Checking:
dir *.mat
You can modify this to select only the MAT file that the user wants, etc.
Henning Schmitz
on 2 Jan 2023
Answers (0)
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!