matlabuser-interfacematlab-guidematlab-gui

MATLAB GUI: updating variables to mat file


I have program where at the end a GUI is launched. I built it using guide. I load 4 variables from my program into 4 GUI text boxes using a mat file called n.mat (and a pushbutton feature).

In the program

n = [nuno, ndue, ntre, nquattro];
save n.mat

In the GUI interface pushbutton

% --- Executes on button press in upload.
function upload_Callback(hObject, eventdata, handles)
% hObject    handle to upload (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

S = load('n.mat');

handles.v1 = S.nuno;
handles.v2 = S.ndue;
handles.v3 = S.ntre;
handles.v4 = S.nquattro;

set(handles.initial1,'String',num2str(handles.v1));
set(handles.initial2,'String',num2str(handles.v2));
set(handles.initial3,'String',num2str(handles.v3));
set(handles.initial4,'String',num2str(handles.v4));

guidata(hObject, handles); 

Then I have other 4 text boxes where I change the value of the variables and save them in another mat file. I'm not sure if I'm doing this correctly.

In the program (before calling myGUI) I initialize the m vector for the updated variables.

nunof = 0;
nduef = 0;
ntref = 0;
nquattrof = 0;
m = [nunof, nduef, ntref, nquattrof];
save m.mat

In the program (after calling myGUI) I try and load the m.mat file and extract the variables from it so I can use them in some calculations further in the program.

load m.mat;

nunof = m.nunof;
nduef = m.nduef;
ntref = m.ntref;
nquattrof = m.nquattrof;

Before this, in the GUI interface 'done' button I try and save my inputs into the m.mat file like this:

function done_Callback(hObject, eventdata, handles)
% hObject    handle to done (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% save the parameters to file

load('m.mat');
m = [nunof, nduef, ntref, nquattrof];
nunof = str2num(get(handles.final1,'String'));
nduef = str2num(get(handles.final2,'String'));
ntref = str2num(get(handles.final3,'String'));
nquattrof = str2num(get(handles.final4,'String'));

save('m.mat','-append');

I want to know why this isn't working and how can I change it. Thanks a lot.


Solution

  • You can't use save('m.mat','-append');. You're missing an option to get to append.

    In order to use append you have to declare a filename, the variable and then append.

    save(filename,variables,'-append')
    

    Taken from - https://au.mathworks.com/help/matlab/ref/save.html

    Also, from your code you're not redefining the variables in your m struct.