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

조회 수: 6 (최근 30일)
Katherine
Katherine 2023년 7월 13일
편집: Jon 2023년 7월 14일
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
Les Beckham 2023년 7월 13일
If you provide a couple of example files you will be more likely to get an answer.

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

답변 (1개)

Jon
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)
myTable = 3×5 table
run speed configuration rpm avgdat ___ _____ _____________ ____ ______ 1 23 {'AX'} 1242 3 19 89 {'QY'} 903 333 22 56 {'FG'} 3234 33

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by