MATLAB Answers

Saving data in a GUI callback function

5 views (last 30 days)
I don't fully understand how to save data acquired through a GUI callback function, so I can use in another callback function. I want to save some variables in one function, everytime it is run, and then when another button is pushed all these data are written to an Excel file. I've tried the guidata function, but I don't really understand how to use hObject or handles. I've also found other functions for this, but it's unclear to me how to use them.
function SnapshotButtonPushed(app, event)
ind = app.SamplenumberEditField.Value;
%% Make picture
im = f_camera();
%% Detection
amount = f_detect
%% Save endtime and concentration to excel file
table_amount{ind} = amount;
table_conc{ind} = concentration;
table_endtimes{ind} = datestr(now,'HH:MM:SS');
hObject= %?
guidata(hObject,struct(ind, table_amount, ind, table_conc, ind,table_endtimes)); %Is this correct?
function ButtonPushed2(app, event)
ind = app.SamplenumberEditField.Value;
table_sample{ind} = ind;
table_name{ind} = 'name';
filename = 'Format.xlsx';
xlswrite('Format.xlsx',table_name,'Sheet1','A2')
xlswrite('Format.xlsx',table_sample,'Sheet1','B2')
xlswrite('Format.xlsx',table_endtimes,'Sheet1','G2')
xlswrite('Format.xlsx',table_amount,'Sheet1','H2')
xlswrite('Format',table_conc,'Sheet1','I2')
winopen('Format.xlsx')

Accepted Answer

Mohammad Sami
Mohammad Sami on 2 Aug 2021
With app designer you don't need guidata and hobject to store your data.
Create properties in your app and then you can access them from inside any app function using app.propertyname.
For more information read the documentation here
https://www.mathworks.com/help/matlab/creating_guis/share-data-across-callbacks-in-app-designer.html
For example
function f1(app)
% .... %
app.table_name = some_table
% .... %
end
function f2(app)
% .... %
xlswrite('Format.xlsx',app.table_name,'Sheet1','A2');
% .... %
end

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!