How to prevent user from changing directory using uiputfile
9 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
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
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
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
2014 年 1 月 30 日
folder='E:\matlab';
fol='';
while ~isequal(folder,fol)
[fil,fol]=uigetfile(folder,message)
end
0 件のコメント
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
2014 年 1 月 30 日
a=listdlg('PromptString','Select a file:','SelectionMode','single','ListString',f)
What other functionality do you need?
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!