Clear Filters
Clear Filters

Search for files in directory, and use file name to input data

10 views (last 30 days)
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
I want to take all lvm files in a specific folder and sort them in this format

Answers (1)

Jon on 14 Jul 2023
Edited: Jon on 14 Jul 2023
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
% Put data into a table, columns will be named the same as the variables
myTable = table(run,speed,configuration,rpm,avgdat)
myTable = 3×5 table
run speed configuration rpm avgdat ___ _____ _____________ ____ ______ 1 23 {'AX'} 1242 3 19 89 {'QY'} 903 333 22 56 {'FG'} 3234 33

Community Treasure Hunt

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

Start Hunting!