How to prevent user from changing directory using uiputfile
7 views (last 30 days)
Show older comments
I would like to allow a user to select a name for a file to be saved using a standard dialog box for saving files, such as uiputfile, but I do not want the user to have the freedom to choose the folder (directory). Is there a way to do this using uiputfile or is there some alternative you can suggest. Note that other parts of my application are depending upon having all of the files saved in the same folder (which I designate) so I can't allow the user to save files into an arbitrary folder. Thanks for your assistance
0 Comments
Accepted Answer
Image Analyst
on 30 Jan 2014
Sure, just ignore what folder they pick.
% Get the name of the file that the user wants to save.
startingFolder = 'c:\'; % Whatever folder you want
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Just try to specify a file in a different folder - I dare you')
if baseFileName == 0
% User clicked the Cancel button.
return;
end
% Create filename using your folder, not theirs.
fullFileName = fullfile(startingFolder, baseFileName);
message = sprintf('I will save your file as:\n%s', fullFileName)
msgbox(message);
Or you could just have an edit field where they type in the filename and you use fullfile() like I did above. Why even have them use uiputfile at all?
5 Comments
Iain
on 3 Feb 2014
Why do you want to restrict them to saving stuff in a single folder?
1. It makes it hard to organise, as a user, if you end up with lots of files.
2. It forces the user to take additional actions should they wish to just save a copy to a different location.
3. If the default folder was sensibly chosen, the user wouldn't really want to change folder.
4. Its more work for you, compared to just popping up a warning box telling them that the file won't be available for use unless they put it in your default location.
Image Analyst
on 4 Feb 2014
I agree with lain. But like I said, you can do it almost like you want if you just throw away their folder that they browsed to, and use the one you want, just like I showed you. If that solves your problem, then mark my answer as "Accepted." As far as I know there is no way to keep them from changing folders if you use the standard save file API call.
More Answers (2)
Azzi Abdelmalek
on 30 Jan 2014
folder='E:\matlab';
fol='';
while ~isequal(folder,fol)
[fil,fol]=uigetfile(folder,message)
end
0 Comments
Azzi Abdelmalek
on 30 Jan 2014
You can use Listdlg function
folder='E:\matlab';
d=dir([folder '\*.m']);
f={d.name};
a=listdlg('PromptString','Select a file:','ListString',f)
file=f(a)
3 Comments
Azzi Abdelmalek
on 30 Jan 2014
a=listdlg('PromptString','Select a file:','SelectionMode','single','ListString',f)
What other functionality do you need?
See Also
Categories
Find more on Environment and Settings 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!