Save data from MatLab GUI to struct .mat file

2 visualizzazioni (ultimi 30 giorni)
Dams
Dams il 11 Set 2019
Commentato: Rik il 12 Set 2019
Hello
I am new to working with GUI's and GUIDE in MatLab, but I am trying to make (what I thought would be) a simple and quick to make GUI for assigning values to audio recordings.
The GUI is to be used for plotting and playing audiosamples of a beating heart, where I should be able to listen to the sample and determine the number of heart beats in the sample. I should then write the number of beats in a 'edit text' box and save the name of the current audiosample together with the number of heart beats in a struct to be used later. I have everything working except saving data to a struct.
My code:
% --- Executes on button press in saveBeats.
function saveBeats_Callback(hObject, eventdata, handles)
% hObject handle to saveBeats (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load('beatData.mat','filename','numberBeats');
currentFileName = getappdata(0,'currFile'); %gets the name of current audiosample
currentBeats = get(handles.numBeats,'Value'); %gets the number of beats written in the edit box
currentIndex = get(handles.listbox1,'Value'); %gets the index of the current audiosample from a listbox
beatData = struct('filename',{},'numberOfBeats',{}); %creation of simple struct with two fields: 'filename' and 'numberOfBeats')
beatData(currentIndex).filename = currentFileName; %saves the name of the audiofile to 'filename'-field in the struct beatData at index
beatData(currentIndex).numberOfBeats = currentBeats; %saves the number of beats
save('beatData.mat', 'beatData'); %save the struct as a .mat file
What I get from this code is a struct saved as 'beatData.mat', however it only saves the last entry. So if I only save the first sample with e.g. 12 heart beats, I get a 1x1 struct with two fields, of the sample name and 12 beats. All good. But if I move on to the next sample I get a 1x2 struct with the fields at the second row filled but the first row empty.
I need a way to save the sample name and number of heart beats to a struct or anything outside the GUI.
I hope my question is clear, otherwise plases ask and I will try to elaborate.

Risposta accettata

Rik
Rik il 11 Set 2019
Modificato: Rik il 11 Set 2019
You should always load to a struct. That makes it clear where variables come from. Then it is also easier to extend the struct instead of creating a new struct every time like you're doing.
% --- Executes on button press in saveBeats.
function saveBeats_Callback(hObject, eventdata, handles)
% hObject handle to saveBeats (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if exist('beatData.mat','file')
S=load('beatData.mat');beatData=S.beatData;
else
beatData = struct('filename',{},'numberOfBeats',{}); %creation of simple struct with two fields: 'filename' and 'numberOfBeats')
end
currentFileName = getappdata(0,'currFile'); %gets the name of current audiosample
currentBeats = get(handles.numBeats,'Value'); %gets the number of beats written in the edit box
currentIndex = get(handles.listbox1,'Value'); %gets the index of the current audiosample from a listbox
beatData(currentIndex).filename = currentFileName; %saves the name of the audiofile to 'filename'-field in the struct beatData at index
beatData(currentIndex).numberOfBeats = currentBeats; %saves the number of beats
save('beatData.mat', 'beatData'); %save the struct as a .mat file
  2 Commenti
Dams
Dams il 12 Set 2019
Ok, so the problem was that whenever I pushed the button to save to my struct, I created a new struct and saved to it, thus discarding the old struct.
This helped me. Thank you for you quick answer!
Rik
Rik il 12 Set 2019
As always, you're welcome, that was indeed the issue.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by