Appdesigner Question: Is it possible for a user to reset a default value?
20 次查看(过去 30 天)
显示 更早的评论
I have a GUI that has a editable text field which specifies a path. This field has a default value (e.g. '~/foo/'). Does app designer allow for a user to reset a default value so that it is recalled when the app is closed and reopened?
For example: Path field = '~/foo/' is not useful for the user and would prefer to have the Path field default to '~/bar/'. But instead of having to change '~/foo/' to '~/bar/' every time the app is opened, I would like to know if the user can reset that value and have it reflected in the app's source code
After some digging around it doesn't seem like this is possible; figured it couldn't hurt to ask here.
Let me know if my question needs clarification. Thanks
1 个评论
Adam
2018-9-7
I generally use a simple user-editable text file for things like this if I want to give the user an option to change e.g. the default range of a slider or value of an edit box.
I create a file with whatever settings in and a simple string for each that tells the user who reads the file what it is (though I add comment lines anyway for some) and that I can read in from the file to initialise my edit box or slider just as I would read a hard-coded value from code.
回答(2 个)
Christian Karcher
2018-9-7
Hey David,
before your question hits its second birthday, let me share my approach to this task:
I indirectly solve this by implementing two functions, app.loadState() and app.saveState() The purpose of these function is to save all relevant variables into an external matfile and reload them upon program start.
saveState only gets called via a "set default values" button or menu, loadState gets called in the startup fcn.
Here's a rough scetch of the code:
function startupFcn(app)
try
app.loadState;
catch % if no default values have been set yet
% set standard values here;
end
end
function SetasdefaultButtonPushed(app, event)
app.saveState;
end
function saveState(app)
state.TextArea.Value = app.TextArea.Value;
state.SomeListBox.Value = app.SomeListBox.Value;
state.myProperty = app.myProperty ;
% [...]
save('MyAppDefaultValues.mat','state');
end
function loadState(app)
load('MyAppDefaultValues.mat','state');
app.TextArea.Value = state.TextArea.Value;
app.SomeListBox.Value = state.SomeListBox.Value;
app.myProperty = state.myProperty ;
%[...]
end
0 个评论
Stephen23
2018-9-7
编辑:Stephen23
2018-9-7
"... allow for a user to reset a default value so that it is recalled when the app is closed and reopened?"
2 个评论
Christian Karcher
2018-9-7
Not to sure about that. You would still require extra variables for each property and GUI element you wish to save and one would always create default values, which is not wanted in most applications (that I use)
Stephen23
2018-9-7
编辑:Stephen23
2018-9-7
"You would still require extra variables for each property and GUI element you wish to save..."
That would be a very verbose approach. Just use one structure.
"...and one would always create default values, which is not wanted in most applications..."
I don't see why this should require "always" creating any values. Create one persistent structure. Add fields as required, which contain the users "default" values. On opening, transfer those fields' data to the internal handles structure (or whatever relevant parameter structure), otherwise use the values from the startup code. Simply put, if the field does not exist then the value has no "saved" default, and handles retains its startup value. Remove the relevant fields if no longer desired to have a "saved" default. See fieldnames, rmfield, dynamic fieldnames, etc.
Not very complex at all, and using persistent is more efficient than your approach of saving/loading .mat files. In fact, what I proposed is really just the same as your answer, just without the .mat files.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!