Matlab tool to extract data from struct to seperate .mat files

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

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?
each mat file has multiple nonscalar struct arrays (time (scalar), values (struct) and name) but this should not be relevant as the tool just has to load in the mat file and then convert this into seperate mat files containing the nonscalar struct arrays.
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.
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.
Please elaborate the question clearly. I will create a GUI for you. Below are my concerns:
  1. Does the MAT file contain only the struct data-types or any other numeric datatypes as well?
  2. How do you want to separate the data and then export it to the other MAT files?
  3. 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.
Hey Rahul, thank you very much!
With the GUI you should be able to do the following steps.
  1. Load in .mat files from your PC
  2. A text field where the name of the selected file is shown
  3. A checkbox with the name "Convert all"
  4. A dropdown menu, where you can select the variables in the struct of the .mat file
  5. A convert button which automatically saves the selected variables (all or the selected one from the dropdown menu) as new .mat files with their corresponding variable name as the new .mat file name
Something like this:
I already got the functions 1 and 2 implemented but i am a bit stuck in the converting of the file so a bit of help would be pretty nice.
function load_data_button_Callback(hObject, eventdata, handles)
% get filename
[file,path] = uigetfile('*.mat');
file_path = fullfile(path,file);
handles.data_name = file;
handles.full_file = file_path;
set(handles.data_name_edittext, 'String', handles.data_name);
guidata(hObject,handles)
function data_name_edittext_Callback(hObject, eventdata, handles)
% Show data name
handles.metricdata.data_name_edittext = data_name;
guidata(hObject,handles)
I will attach a .mat file which is comparable to the ones which i will have to convert. So the task would be to get new .mat files X and Info which are containing the informations from rec1_002.X and rec1_002.Info.
As Jan wrote, LOAD the MAT-file and loops over its content using FIELDNAMES():
S = load('rec1_002.mat')
S = struct with fields:
Info: [1×1 struct] X: [1×1 struct]
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
Info.mat X.mat rec1_002.mat
You can modify this to select only the MAT file that the user wants, etc.

Sign in to comment.

Answers (0)

Asked:

on 28 Dec 2022

Commented:

on 2 Jan 2023

Community Treasure Hunt

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

Start Hunting!