MATLAB: How to prevent user from changing directory using uiputfile

file

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

Best 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?