input directory same as output directory - xlswrite

2 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 17 Sep 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 Comments
Image Analyst
Image Analyst on 17 Sep 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)

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!