Using App Designer, saving (and loading) data from the Edit Fields of the GUI to a file?

64 views (last 30 days)
I have several numeric edit fields in my gui. How do I go about saving the entered data to a text file?
I want to be able to save the entered values for the above Center Freq and Bandwidths to a file so I can recall them at a later time. Those fields are app.NumericEditField_CenterFreq1, app.NumericEditField_Bandwidth1, app.NumericEditField_CenterFreq2, etc.
I have save and load buttons, and understand I need to place code in the call backs of those buttons, but I haven't been able to find any good examples. I tried the below, but it saves it to a .mat file, which is unreadable in a text error. Is there an easier way to save and then load the data to and from my edit fields?
function SaveButtonPushed(app, event)
props = properties(app);
lp = length(props);
values = cell(1,lp);
visibilities = cell(1,lp);
for i = 1:lp
propName = props{1};
property = app.(propName);
if isprop(property, 'Value')
values{i} = app.(propName).Value;
end
end
file = uiputfile('*.mat', "Save Message" );
if file
save(file, 'props', 'values', 'visibilities');
end
The above saves data, but not sure if it is correct since it is unreadable. When I try and load, I dont get anything in the edit fields:
function LoadButtonPushed(app, event)
[file,path] = uigetfile('*.mat');
selectedfile = fullfile(file);
data=load(selectedfile);
end
Thanks in advance!

Accepted Answer

Rik
Rik on 23 Apr 2023
Your loading function needs to load the file, and then set the values of the fields. It currently only does the former.
A mat file is not a text file. You can examine the contents with Matlab (or even Octave), but not with text readers. If you want a text file, you may consider converting to JSON.
  11 Comments
Rik
Rik on 24 Apr 2023
That looks like it should work as you expect.
What I don't understand is why you convert the char to double before writing to your JSON file. Wouldn't it make sense to store them in the same way the editfields hold them? That way they will end up as a string in your JSON file, but that will be the exact value that will be restored to your GUI.
Scotty Mac
Scotty Mac on 24 Apr 2023
I guess you are right. I dont need to convert them to doubles since I am not performing any calculations on them when I save/load. I'll change it.
Thanks for all the help, appreciate it!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!