How to prevent user from changing directory using uiputfile

9 ビュー (過去 30 日間)
Jon
Jon 2014 年 1 月 30 日
コメント済み: Image Analyst 2014 年 2 月 4 日
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

採用された回答

Image Analyst
Image Analyst 2014 年 1 月 30 日
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 件のコメント
Iain
Iain 2014 年 2 月 3 日
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 2014 年 2 月 4 日
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.

サインインしてコメントする。

その他の回答 (2 件)

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

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 30 日
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 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 30 日
a=listdlg('PromptString','Select a file:','SelectionMode','single','ListString',f)
What other functionality do you need?
Jon
Jon 2014 年 1 月 30 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by