How to save current file loaded in app after using uigetfile?
8 views (last 30 days)
Show older comments
I'm using app designer (R2022b) and I have a callback that allows the user to load a file. The user can click the load option at anytime to load in a new file, HOWEVER if the user decides after clicking the load option that they want to stick with the currently selected data rather than load new, if they click cancel or close the uigetfile=0 and I've set up the app to disable the other features except the Load option.
Is there a way to store the data file name such that I could create an IF statement (or similar) if they decide to keep working on the current file they don't have to reselect it, they can simply click cancel or close and resume working?
I've tried a logical, but it appears the filename is erased as soon as the callback is performed ergo I cannot use a logical to compare the old filename to the uigetfile option since the uigetfile is cleared after the callback is performed.
0 Comments
Accepted Answer
Voss
on 15 Nov 2023
Edited: Voss
on 15 Nov 2023
You can make an app property, say current_filename, and store the name of the currently selected file in that property.
Then in your callback, update app.current_filename when the user selects a file.
function callback(app,evt)
[fn,fp] = uigetfile('','Select A File',app.current_filename); % <- you can use app.current_filename
% as the default location/name for
% uigetfile to open to
if isequal(fn,0) % user cancelled -> keep using the same
% current_filename you are already using
return % -> nothing to do -> return early
end
% update app.current_filename
app.current_filename = fullfile(fp,fn);
% then do whatever updates you need to do
% (read the file, update the GUI and any
% app data you need to update, etc.)
end
Note that this callback doesn't "disable the other features except the Load option" since there is no need to do that, in my opinion. If the user clicks cancel or close on uigetfile, then they stay with the previously-selected current file, whatever it is. It's still stored in app.current_filename, and the app is still in a state corresponding to that file (since the app was updated when the file was selected previously).
[However, you may have a state in which you should in fact "disable the other features except the Load option", and that would be when app.current_filename is not an existing file. For instance, you can iniitalize app.current_filename to be the empty character array (''), and initialize your GUI to have everything disabled except the load button, which forces the user to select a file before doing anything else. When a file is selected, other GUI components become enabled. You can use isfile(app.current_filename) to determine whether the file exists.]
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!