MATLAB: Save data from MatLab GUI to struct .mat file

callbackguiguidehandlesloadMATLABsavestruct

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 1×1 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 1×2 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.

Best Answer

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