Auto grab files based on file name

4 views (last 30 days)
Tyler
Tyler on 20 Dec 2022
Answered: cui,xingxing on 21 Dec 2022
The code below changes the raw file to txt file. However, I want to have the code auto grab the file named "2022-09-07_003_DR06 PRE-TEST_HORIZONTAL_NOM_" instead of a new dialog box opening up. I have files that are basically the same but named POST instead of PRE or VERTICAL instead of HORIZONTAL so if I can get it to auto grab based on naming then I can apply that to all the different types of files.
Something like '*PRE-TEST_VERTICAL*NOM.txt' works in a similar code but I can't get it to adjust to the code below,
% Loop through each file, copy it and give new extension: .txt
for i = 1:numel(fileList)
file = fullfile(directory, fileList(i).name);
[tempDir, tempFile] = fileparts(file);
status = copyfile(file, fullfile(tempDir, [tempFile, '.txt']));
end
% Enter the directory to search for newly created text files of the data
[file_list, path_n] = uigetfile('.txt','Grab the files you want to process','MultiSelect','on');
if iscell(file_list) == 0;
file_list = {file_list};
end
%Starts a figure that will hold until all files have been processed
figure
hold on
for i = 1:length(file_list)
filename = file_list{i};
data_in = readmatrix([path_n filename]);
%Grabbing only the frequency and SE values
x = data_in(:,1);
y = data_in(:,3);
end

Answers (1)

cui,xingxing
cui,xingxing on 21 Dec 2022
hi,
you can try use datastore or fileDatastore instead of uigetfile to get all your file names,then use "string" class methods to handle each your file names,match pattern ,find ,replace specification character is easy.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!