Search for files in directory, and use file name to input data
6 次查看(过去 30 天)
显示 更早的评论
I have a buncch of files in a folder, and the names of the files mean something. I want to get the files into matlab, find the data in the file and add it to a table/array or even output as an excel the data and use the file name to make columns that correspond.
So each file is called R0XX_YYms_ZZ_AAAArpm.lvm, where XX, YY, and AAAA are different numbers and ZZ are letters. I want MATLAB to find the files and use the name of the file to make an output that looks like:
Run number Speed Configuration RPM Average from data in the file
XX YY ZZ AAAA BBBB
I want to take all lvm files in a specific folder and sort them in this format
1 个评论
Les Beckham
2023-7-13
If you provide a couple of example files you will be more likely to get an answer.
回答(1 个)
Jon
2023-7-14
编辑:Jon
2023-7-14
I think this example shows you how you could solve your problem
% Find all of the relevant files
% for this example I used Excel files, because I don't
% know what a .lvm file is but you would substitute .lvm
list = dir("RO*.xlsx");
% Preallocate variables to hold data
numfiles = numel(list);
run = zeros(numfiles,1);
speed = zeros(numfiles,1);
configuration = cell(numfiles,1);
rpm = zeros(numfiles,1);
avgdat = zeros(numfiles,1);
% Loop through list analyzing data and storing it in table
for k = 1:numfiles
% Parse out relevant data from file name
parts = strsplit(list(k).name,{'_','.'});
run(k) = str2double(parts{1}(3:end));
speed(k) = str2double(parts{2}(1:2));
configuration{k} = parts{3}; % use curly braces on left side for cell array of strings
rpm(k) = str2double(parts{4}(1:4));
% Read the data in the file and compute average
% In this example there is just one column of data in file
% you would have to elaborate this to match your actual file contents
% and format
dat = readmatrix(list(k).name);
avgdat(k) = mean(dat(:)); % use (:) to make sure it is a column
end
% Put data into a table, columns will be named the same as the variables
myTable = table(run,speed,configuration,rpm,avgdat)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!