Clear Filters
Clear Filters

Saving data using uiputfile using user defined path

32 views (last 30 days)
Hello all, we are retireving the result from the GUI developed by us and those results are stored in NOTEPAD with three different filenames, i should provide facility to the user to choose the path and it has to choose same path as default for the next time. i have provided the pseudocode
[fileName,pathName] = uiputfile('Coefficients.h'); file_path = fullfile(pathName,fileName); fid1 = fopen(file_path, 'wt');
[fileName,pathName] = uiputfile('parameters.h'); file_path = fullfile(pathName,fileName); fid1 = fopen(file_path, 'wt');
so for saving Coefficients.h I choose the path, while saving parameters.h, pathname should be retrieved from previous
  3 Comments
sandeep singh
sandeep singh on 8 Oct 2018
Dear Adam, i need facility where user should not face problem to choose path every-time to save the file, it should take to directories previously chosen
Adam
Adam on 8 Oct 2018
Yes, that is why I said use the output from the previous call to uiputfile. If you don't want to give the user a choice even then you can just do away with the later uiputfile calls, but if you want to give them the option but default it to their previous selection then you can give the full filepath by concatenating your next filename with the previous path and pass this to uiputfile.

Sign in to comment.

Answers (1)

Jan
Jan on 8 Oct 2018
Edited: Jan on 8 Oct 2018
Either:
[fileName, pathName] = uiputfile('Coefficients.h');
[fileName2, pathName2] = uiputfile('parameters.h', 'Choose a file', ...
fullfile(pathName, '\'));
(The fullfile commands forces the pathName to have a trailing separator. I'm not sure if this is guaranteed after uiputfile.)
or
[fileName, pathName] = uiputfile('Coefficients.h');
[fileName2, pathName2] = uiputfile(fullfile(pathName, 'parameters.h'));
Now the folder chosen by the first uiputfile is used by the 2nd one also. You can do this automatically also:
function [F, P] = uiPutFileStay(File, Str, Folder)
persistent oldfolder
if isempty(oldFolder)
oldFolder = fullfile(cd, '\');
end
if nargin < 3
Folder = oldFolder;
end
[F, P] = uiputfile(File, Str, Folder);
if ischar(P)
oldFolder = fullfile(P, '\');
end
end
Now call this with 2 inputs to reuse the formerly used folder.

Categories

Find more on Scope Variables and Generate Names 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!