input directory same as output directory - xlswrite

2 visualizaciones (últimos 30 días)
Daan
Daan el 17 de Sept. de 2015
Comentada: Image Analyst el 17 de Sept. de 2015
Hi, I have got hundreds of excel files in different folders which I want to run through a script and then save in the same folder. So basically I want to choose the input file, press the run button and then I want the output file in the same folder as the input file. What is the best way to do this?

Respuesta aceptada

Walter Roberson
Walter Roberson el 17 de Sept. de 2015
Normally if you are "choosing" an input file, you are using uigetfile(). That has two outputs, the file name and the file directory. You would use fullfile() to create the complete file name of the file to be read, and you would use fullfile() with the same directory and a different name to create the file name to write to.
If you have a lot of files to process you may wish to use the uigetfile() 'Multiselelect' 'on' option. When you use that, the file name output is a cell array of strings and the directory name output is a single string. You would then loop through all of the entries in the cell array of strings doing the same as above.
Alternately you may wish to process everything in a particular directory. You can use uigetdir() to have the user select a directory name. After that... http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
  3 comentarios
Daan
Daan el 17 de Sept. de 2015
I worked it out. Many Thanks!
Image Analyst
Image Analyst el 17 de Sept. de 2015
Try this snippet:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by