How to prevent user from changing directory using uiputfile

9 views (last 30 days)
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

Accepted Answer

Image Analyst
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
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
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.

Sign in to comment.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 30 Jan 2014
folder='E:\matlab';
fol='';
while ~isequal(folder,fol)
[fil,fol]=uigetfile(folder,message)
end

Azzi Abdelmalek
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
Azzi Abdelmalek on 30 Jan 2014
a=listdlg('PromptString','Select a file:','SelectionMode','single','ListString',f)
What other functionality do you need?
Jon
Jon on 30 Jan 2014
Basically this is being used for a file save/save as function. When the user enters a name that already exists he needs to be prompted and asked if the existing file should be replaced. Otherwise the file is saved with a new name. So of course I can recreate all of this, but what I wanted was all the built in functionality of uiputfile, along with the standard OS look and feel, so I really wanted to put up the standard file save dialog box and just lock down the navigation to other folders. Thanks for all your ideas.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!