필터 지우기
필터 지우기

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

조회 수: 38 (최근 30일)
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.
  댓글 수: 11
Rik
Rik 2023년 4월 24일
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 2023년 4월 24일
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!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by