How to prevent user from changing directory using uiputfile

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

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

Thanks for your reply. Your code does ultimately prevent the user from changing the directory, but the user can still navigate to other locations before getting bounced back and this may be confusing and aggravating. I was hoping for a way of turning of the ability to browse (change folders) within the file selection dialog
I don't understand why you want to show them a save dialog box and let them see the directory tree if they can't change folders. What's the point? Seems like a cruel trick/hoax on your user to me. Could be frustrating for them.
Basically the answer, as far as I know, is No - you can't "lock down" the folder in uiputfile.
Even if they can't change their location on the directory tree there is a lot of nice functionality built into the standard windows OS save dialog box. It also looks familiar and professional. While its not too hard to recreate the functionality of the standard save dialog (I have now done that already) it has a kind of "home made" look to it. As far as a cruel trick/hoax, I think if the top drop down list where you change directories were grayed out (or better just not there) it would be clear to the user that they had to stay in the current directory. Anyhow thanks for all of your ideas and work on this.
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.
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)

folder='E:\matlab';
fol='';
while ~isequal(folder,fol)
[fil,fol]=uigetfile(folder,message)
end
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

I am doing something like this now as a work around, but I really want to have all the nice functionality that is built into the standard dialog box for selecting files including checking if file already exists, displaying the file attributes, and just the look and feel of the standard operating system file selection. Yes I could rebuild all that too, but I was really hoping to just lock down/disable the directory browsing on the standard window. Same comments for other suggestions.
a=listdlg('PromptString','Select a file:','SelectionMode','single','ListString',f)
What other functionality do you need?
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

Tags

Asked:

Jon
on 30 Jan 2014

Commented:

on 4 Feb 2014

Community Treasure Hunt

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

Start Hunting!