input directory same as output directory - xlswrite

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?

 채택된 답변

Walter Roberson
Walter Roberson 2015년 9월 17일

0 개 추천

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

Daan
Daan 2015년 9월 17일
Thanks for the quick help. I am indeed working with the uigetfile() function to read in one excel file at the time. But what is the difference between the fullfile() output and the directory output of the uitgetfile() function? And how should I use that to write my output file in the same directory as the input file?
Something like xlswrite('directory', 'filename','matrix')?
Thanks in advance
Daan
Daan 2015년 9월 17일
I worked it out. Many Thanks!
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)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품

질문:

2015년 9월 17일

댓글:

2015년 9월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by