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

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!

 채택된 답변

Rik
Rik 2023년 4월 23일
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.

댓글 수: 15

Thanks!
So JSON seems the way to go... Are there any GUI examples were values are saved and loaded into the Edit Fields?
Since you're saving a list of properties and values, isn't the solution fairly straightforward?
values{i} = app.(propName).Value;
This was used to retrieve the value, so it seems easy enough to invert this:
app.(propName).Value = values{i};
What did you try yourself?
I just don't know how to pass the Properties and values to a JSON file format and save. I will give it a shot.
I got the load part, just not the save/JSON part.
Thanks!
The jsonencode and jsondecode functions will do almost all the work for you. Then you only need to learn how to write text to a file. If you don't know that already, I suggest you do a basic Matlab tutorial. After that, you will know to look for the fopen, fprintf, and fclose functions.
OK thanks for the help! I will give it a try for sure.
I can't figure out how to convert my editfields to a JSON structure.
If my first edit field is called CenterFreq1 and contains the value 1500:
CenterFreq1 = str2double(app.EditField_CenterFreq1.Value); %works correctly to place 1500 into the variable
data = ['CenterFreq1', CenterFreq1];
jsonData = jsonencode(data);
It doesn't create the json structure. If I try:
jsonData = jsonencode(data, 'CenterFreq1', true);
I get an exception. I thought I could name the variable 'CenterFreq1'.
I want the JSON to look something like:
{"CenterFreq1":1500,"CenterFreq2":0}
I can write the value to a file, just the grabbing of the values and conversion is my issue at the moment.
Do I need to do something like?
panelStruct=struct();
panelStruct.name1="CenterFreq1";
panelStruct.value1 = str2double(app.EditField_CenterFreq1.Value);
panelStruct.name2="CenterFreq2";
panelStruct.value2 = str2double(app.EditField_CenterFreq2.Value);
panelStructText = jsonencode(panelStruct);
I don't understand what you're trying to attempt.
Of course that syntax doesn't create the JSON structure. It concatenates the char vector CenterFreq1 with the value in the variable named CenterFreq1, converting it to char.
Why don't you only change the saving/loading part?
panelStruct = struct('props',props, 'values',values, 'visibilities',visibilities);
txt = jsonencode(panelStruct);
% put code here to write txt to a file
I am trying to grab the values out of the editfields (CenterFreq1, CenterFreq2, etc.) and save them with my 'save button' and in a clear readable text file, like:
{"editfield1":"CenterFreq1","editfield1_value":1500,"editfield2":"CenterFreq2","editfield2_value":2500}
Then load that file to populate the those edit fields (load button).
I dropped the ('props',props, 'values',values, 'visibilities',visibilities) objects, since they didn't hold my values. I want the easiest and most straight forward way.
This what I have so far...
Then for my load, I can just assign the CenterFreq1 back to my editfield after reading in the file (not implemented yet). Is this the correct method to do the load/save?
CenterFreq1 = str2double(app.EditField_CenterFreq1.Value);
CenterFreq2 = str2double(app.EditField_CenterFreq2.Value);
panelStruct=struct();
panelStruct.CenterFreq1 = CenterFreq1;
panelStruct.CenterFreq2 = CenterFreq2;
panelStructString = jsonencode(panelStruct);
% Write to a json file
fid = fopen('test1.json', 'w');
fprintf(fid, '%s', panelStructString);
fclose(fid);
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.
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!
Hi, what did you finally do for your code to work. I am currently facing the same difficulty. Thank you
Hello,
I am using JSON for loads and saves. Here is my Load button call back function:
function LoadButtonPushed(app, event)
% Allow the user to select any JSON file
[file,path] = uigetfile('*.json');
if isequal(file,0)
% User selected cancel
return;
else
jsonText = fileread(fullfile(path,file));
end
% Convert JSON formatted text to MATLAB data types
jsonData = jsondecode(jsonText);
app.EditField_NotchCenterFreq1_LB.Value = num2str(jsonData.NotchCenterFreq1_LB);
app.EditField_NotchBandwidth1_LB.Value = num2str(jsonData.NotchBandwidth1_LB);
end
Here is my Save button function:
I grab the values from the entry fields, put them into variables, place them in a struture, and then write to a JSON file.
function SaveButtonPushed(app, event)
% Grab the value of the Waveform dropdown selection for Low Band
waveform_LB = app.WaveformDropDown_LB.Value;
% Grab the value of the selected radio button for Low Band
timingProtocol_LB = app.TimingProtoclButtonGroup_LB.SelectedObject.Text;
% Create the Json Structure
panelStruct=struct();
panelStruct.WaveformDropDown_LB = waveform_LB;
panelStruct.TimingProtocol_LB = timingProtocol_LB;
% Encode the structure into a JSON string
panelStructString = jsonencode(panelStruct);
% Write to a json file (Prompt user for filename)
[filename, pathname] = uiputfile('*.json','Save Parameters');
path_file = fullfile(pathname, filename);
fid = fopen(path_file,'wt');
fprintf(fid, '%s', panelStructString);
fclose('all');
end
Thank you very much. It worked

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2023년 4월 23일

댓글:

2024년 10월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by