In app designer how do I make a button to save, that saves my data from the different uitables and editfields? And the then afterwards make a button that loads it back in?

21 views (last 30 days)
Hi
Im looking for a tutorial on how to make a save and load button. Where I first can save my data to a file, and then afterwards load it again. I have a UITable where I need to save the data, and some editfield where I also want to save them. Afterwards I want to be able to start the program and then load them in.
Please help me:
uisave()
Doesnt help me that much.
Best regards Mikkel

Accepted Answer

Selby
Selby on 11 Aug 2017
Edited: Selby on 11 Aug 2017
Here is some code that I have used in the past to do something very similar to what you desire. It is a code that I wrote to help me explain matlab to a friend. It is broken down into three functions a main, load and save. I recommend having a play with it and investigate the save function to load and save an additional object. It saves a generic .mat file with a different extension of your choosing and includes both global parsed and local variables. Even a try catch is thrown in for fun!
function main()
global extensionString
extensionString = 'table';
fig = figure;
mytable = uitable(fig,'units','normalized','position',[0.01,0.01,0.5,.98],'Data',randi(100,30,3),'ColumnEditable',true);
uicontrol(fig,'units','normalized','position',[0.6,0.6,0.3,.2],'string','Save','callback',@(s,e)savetabledata(s,e,mytable))
uicontrol(fig,'units','normalized','position',[0.6,0.3,0.3,.2],'string','Load','callback',@(s,e)loadtabledata(s,e,mytable))
end
function savetabledata(s,e,mytable)
global extensionString
savedData = mytable.Data;
[FileName,PathName] = uiputfile(['mytable.',extensionString],'Save table data');
save([PathName,FileName],'savedData');
end
function loadtabledata(s,e,mytable)
global extensionString
[fileName, pathName] = uigetfile(['*.',extensionString],'Load Table File','MultiSelect', 'off');
if fileName==0
return;
end
try
load([pathName,fileName],'-mat');
catch
errordlg(['There was a problem loading the .',extensionString,' file'],'Load Error!');
return;
end
set(mytable,'Data',savedData);
end
  1 Comment
Nguyen Thuan
Nguyen Thuan on 4 Aug 2020
Edited: Nguyen Thuan on 4 Aug 2020
Hi Selby! What is s and e in your main function at @(s,e)savetabledata(s,e,mytable)?
Could you also explain why is the role of the main function in the program?
Thanks a lot!

Sign in to comment.

More Answers (1)

Cyril Torre
Cyril Torre on 6 Oct 2020
Edited: Cyril Torre on 6 Oct 2020
I think the solution from Selby is the best, but I am giving my solution which is an alternative, maybe not optimised but easy to do it.
My solution would be to save everything in csv file in a folder and then load them after that. I will present you an example of what I got to save data.
Here the code to open a file, saved in the folder data, and called apdData_5mm_20201006 (because the current date is 06/10/2020 and zposition is a global variable defined by another function. But It does not matter you put what you want.
global zposition
current_date = datestr(now,'yyyymmdd_HHMMSS');
filename = sprintf('data/apdData_%smm_%s.csv', zposition, current_date );
fileIDapd = fopen(filename,'w');
fheader = 'z_sample, det_1, det_2, coinc, Klyshko1, Klyshko2, data_per_pixel, temperature, texp';
fprintf(fileIDapd,'%s\n', fheader);
fclose(fileIDapd);
Then to write I use the following code:
dlmwrite(filename, [posdata(1,1), apd1(j), apd2(j), coins(j),coins02(j),coins20(j), ...
dpp, temperature, TimeExp], ...
'delimiter', ',','precision', 16,'-append')
Im writing data every row, and this code is in a loop, where 'j' is the index for the row. posdata, apd1, coins, etc are my variables that I want to write on every column. For example in the 5th column, called Klyshko1 (previous code), my code will write under this column the variable coins02(j) for the j^th row
I am doing this because I am measuring data and recording them in the same time with my Matlab app desinger. But you can easily adapt it without the measuring step and just save it.
Obviously it's not the optimised way to do it but it's very easy to do it.
To load data, you just need to read this excel sheet and give every value to each cells?

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!