Run multiple data files from a directory using function in .m file
2 次查看(过去 30 天)
显示 更早的评论
I am a beginner to coding, so I need clear explanations of any possible answers to my question.
I have a folder (labeled as 'FolderName' in the script below) that is full of many subfolders with names like "2YYY-MM-DD". Each of those "YYYY-MM-DD" subfolders contain a data file with the extention ".eod". I have a function in a .m file (labeled as 'function_name' in the script below). What I am wanting to do is run the 'function_name' script for each of those .eod files. The 'function_name' script creates a few plots, and I want each of those plots to be saved into those "YYYY-MM-DD" original folders. Instead of having to run .eod file by file, I want it done in one swoop. This code below is not spitting back errors, but it is also not saving those plots in those respective folders. The 'function_name' script runs perfectly if I simply do the command line "function_name('data_file_name.eod')".
Help would be greatly appreciated!
clear all
close all
parent = '\FolderName';
folders = dir([parent, '\2*']);
folders = char({folders.name});
fileNames = {};
count = 1;
for i = folders
files = dir([parent, i, '\*.eod']);
fileNames(count) = {files.name};
count = count + 1;
end
fileNames = string(fileNames);
for i = fileNames
function_name(char(i));
end
0 个评论
采纳的回答
Image Analyst
2022-4-4
编辑:Image Analyst
2022-4-4
You can't do it all in one swoop. You're going to have to have a loop and open each .eod file and process it one at a time. Tomething like this, adapted from the FAQ:
% Specify the folder where the files live.
topLevelFolder = '\FolderName'; % or wherever, like 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(topLevelFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', topLevelFolder);
uiwait(warndlg(errorMessage));
topLevelFolder = uigetdir(); % Ask for a new one.
if topLevelFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
% The double * is the key to recursing into subfolders.
filePattern = fullfile(topLevelFolder, '**/*.eod'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
% Optional : If you want all the full file names in one variable, do the line below.
allFileNames = fullfile({theFiles.folder}, {theFiles.name})';
for k = 1 : length(theFiles)
% Get the name of this file.
baseFileName = theFiles(k).name;
thisFolder = theFiles(k).folder;
fullFileName = fullfile(thisFolder, baseFileName); % or allFileNames{k}
fprintf(1, 'Now reading file: %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in and plotting something.
% Now save the plot in a PNG folder in the same folder.
[~, baseFileNameNoExt, ~] = fileparts(baseFileName);
outputFullFileName = fullfile(thisFolder, [baseFileNameNoExt, '.png']);
fprintf(1, 'Now saving plot : %s\n\n', outputFullFileName);
exportgraphics(gca, outputFullFileName);
end
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!