input directory same as output directory - xlswrite
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Walter Roberson
2015-9-17
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 个评论
Image Analyst
2015-9-17
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!