Save/load button in gui

4 views (last 30 days)
gabri9455
gabri9455 on 8 Jun 2018
Answered: Walter Roberson on 8 Jun 2018
I'm developing a GUIDE tool and I would like to add a button to save the parameters that in my opinion are correct, that are inside various textbox. Then another button to load the saved file in the corresponding text box. The saved file could be of any type: txt, excel or whatever
Is it possible?

Accepted Answer

Walter Roberson
Walter Roberson on 8 Jun 2018
Sure
function save_params_Callback(hObject, event, handles)
names_to_save = {'editbox1', 'editbox2', 'velocity'};
num_to_save = length(names_to_save);
data_to_save = cell(num_to_save, 2);
for K = 1 : num_to_save
thisname = names_to_save{K};
thisvalue = get(handles.(thisname), 'String');
data_to_save{K,1} = thisname;
data_to_save{K,2} = thisvalue;
end
save('saved_params.mat', 'data_to_save');
To load:
function load_params_Callback(hObject, event, handles)
datastruct = load('saved_params.mat', 'data_to_save');
data_to_save = datastruct.data_to_save;
for K = 1 : size(data_to_save,1)
thisname = data_to_save{K,1};
thisvalue = data_to_save[K,2};
if isfield(handles.(thisname)) && isa(handles.(thisname), 'uicontrol')
set(handles.(thisname), 'String', thisvalue);
end
end

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!