Saving file with file name from user

I have code for push button where user will click on save, then a folder browser opens where user selects the folder to save the file.
Now here my code:
filename=('output.xls');
foldername = uigetdir;
% ensure the folder name is valid
if ischar(foldername)
% update the file name to include the folder name
filename = fullfile(foldername,filename);
% open the file
fid = fopen(filename,'w');
if fid>0
% write the data to file
fprintf(fid, '%6s\t%6s\t\n', 'x', 'y');
fprintf(fid, '%6.4f\t%6.41f\t\n', result);
fclose(fid);
end
end
So here by default the output file name will be output.xls
I want to have the user capability to set the name of the file. So how to do that?

 Respuesta aceptada

Image Analyst
Image Analyst el 30 de Abr. de 2015
Let the user type it in with uiputfile(). Here is my snippet for that:
% Get the name of the file that the user wants to save.
% Note, if you're saving an image you can use imsave() instead of uiputfile().
startingFolder = userpath
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)

9 comentarios

Hi, thanks for the answer but where to put the values I want save? Like in my code
% write the data to file
fprintf(fid, '%6s\t%6s\t\n', 'x', 'y');
fprintf(fid, '%6.4f\t%6.41f\t\n', result);
fclose(fid);
writes data, so like in your code, where to put that part?
Right after it.
% Get the name of the file that the user wants to save.
% Note, if you're saving an image you can use imsave() instead of uiputfile().
startingFolder = userpath
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
fid = fopen(fullFileName, 'wt');
if fid ~= -1
% write the data to file
fprintf(fid, '%6s\t%6s\t\n', 'x', 'y');
fprintf(fid, '%6.4f\t%6.41f\t\n', result);
fclose(fid);
else
warningMessage = sprintf('Cannot open file:\n', fullFileName);
uiwait(warndlg(warningMessage));
end
adi kul
adi kul el 30 de Abr. de 2015
Awesome! That worked!
adi kul
adi kul el 30 de Abr. de 2015
If I want to save .xls file then what will be the changes?
Nova Trimble
Nova Trimble el 1 de Mayo de 2020
May I ask why this won't work for an excel file format as well? I attempted it and it keeps returning the excel file in the folder that the matlab program is being executed.
Image Analyst
Image Analyst el 1 de Mayo de 2020
It does work for any file. It doesn't return an Excel file, it returns a string with the filename that you can then use when you call writetable(), writecell(), xlswrite() or whatever. If the resulting output file is in your current folder, then you're either specifying the current folder, or else you're using only the base file name, not the FULL file name which includes the folder. Show your lines of code if you want me to fix them.
Adi Purwandana
Adi Purwandana el 28 de Feb. de 2023
Hello, your solution's really great...
Anyway, I tried this on my lines:
ISW = array2table(....); % my output variables
startingFolder = 'D:\';
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
writetable(ISW,fullfile(folder,baseFileName)); % but this give me *txt file format, what if I want in xlsx format?
Any solution?
Adi Purwandana
Adi Purwandana el 28 de Feb. de 2023
I found the solution already... just type '*.xls' to replace '*.*' thank you.
Image Analyst
Image Analyst el 28 de Feb. de 2023
@Adi Purwandana These days we use .xlsx rather than .xls to ensure you get the modern format of the workbook.

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 30 de Abr. de 2015
Editada: Stephen23 el 30 de Abr. de 2015

2 votos

It depends on how you want them to enter the filename:
  • in a GUI, then try uiputfile
  • as a variable, then add that variable to the function's input arguments.
  • via the command line, then use input to request user input.
  • within a custom UI, then use uicontrol 'edit' box.

3 comentarios

adi kul
adi kul el 30 de Abr. de 2015
Editada: adi kul el 30 de Abr. de 2015
Hi, can you give me in the form of code?
I have made these changes
[fileName,PathName] = uiputfile('*.xls','Save Results As');
% update the file name to include the folder name
filename = fullfile(filename,PathName);
% open the file
fid = fopen(filename,'w');
if fid>0
% write the data to file
fprintf(fid, '%6s\t%6s\t\n', 'x', 'y');
fprintf(fid, '%6.4f\t%6.41f\t\n', result);
fclose(fid);
end
Now I get a windows to save file but after clicking "save" it doesn't save anything!
Stephen23
Stephen23 el 30 de Abr. de 2015
Editada: Stephen23 el 30 de Abr. de 2015
The path and file arguments around the wrong way in the fullfile call, it should be:
filename = fullfile(PathName,filename);
When you have a problem like this it helps to use the debugging tools, as mistakes like this are easy to find when you check the variables during line-by-line code evaluation.
Note also that you specify the file extension as .xls, but you have opened a textfile and are using fprintf to write text data. This does not make any sense, as .xls files are not text files, but binary files.
You need to decide if you want to work with textfiles or excel files, and then either:
  • use a file extension that is suitable for textfiles and will not be mistaken for an .xls file.
  • save an .xls file using xlswrite.
adi kul
adi kul el 30 de Abr. de 2015
Hi, Thank you but still it's not creating any file!

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Abr. de 2015

Comentada:

el 28 de Feb. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by