Clear Filters
Clear Filters

Loop cycle to input similar txt files

2 views (last 30 days)
Simone
Simone on 8 Dec 2022
Edited: Image Analyst on 10 Dec 2022
Hello everybody, I'm trying to solve a problem to input data. I have 39 txt files that have similar names. The name has two variables and I'd like to import the data as a function that contains those two numbers.
So a general name of the file is "CV_d{m}_die{i}", i goes from 1 to 13 and m is equal to 100, 200 or 400.
I've done this
input_folder = 'folder'
files = dir(fullfile(input_folder, "*.txt"))
file_paths = fullfile({files.folder}, {files.name})
for i = 1:13
for m = [100 200 400]
Data_{m}_{i} = dlmread("CV_d{m}_die{i}.txt", ',', 4, 0)
end
end
So my idea was to do this loop and get 39 Data_m_i. Like Data_100_1, Data_200_1, Data_400_1, and then i=2...
But I get an error that says that the file could not be opened because there's no such file or directory.

Accepted Answer

Jan
Jan on 8 Dec 2022
Edited: Jan on 8 Dec 2022
Data_{m}_{i} is your invention an no valid Matlab syntax. This is not the way programming works.
Matlab does not magically modifiy the string "CV_d{m}_die{i}.txt" to what you want. Why do you assume that the curly braces let Matlab inserts the numbers?
Use a command to create a specific string:
dlmread(sprintf('CV_d%d_die%d.txt', m, i), ',', 4, 0);
Why do you create a list of filenames, which is not used?
Data_1_100 is a bad choice for the name of a variable. Hiding information in the name of a variable makes it hard to access it later. Store such important information such, that you can use the later:
k = 0;
for i = 1:13
for m = [100 200 400]
file = fullfile(folder, sprintf('CV_d%d_die%d.txt', m, i));
k = k + 1;
Data(k).value = dlmread(file, ',', 4, 0);
Data(k).m = m;
Data(k).i = i;
end
end
Hiding information in names of variables is a typical design error made by beginners. See this exhaustive explanation, why other methods should be preferred: TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)
  8 Comments
Simone
Simone on 10 Dec 2022
Sorry I didn't understand that I had to remove those first lines, yes it perfectly works.
Doing so I get a table that has the imported data associated with each m and i value. But if I want to work with the second column of the data associated to m=100 and i=2 for example, how am I supposed to specify that particular data-file in the script?
Clicking to see the table of a particular imported data says the name Data with a number on parenthesis
Jan
Jan on 10 Dec 2022
"Sorry I didn't understand that I had to remove those first lines" - I do not understand. There is no need to remove the first lines, they are just useless - as long as the folder name is set correctly.
If you want the values for m=3 and i = 200:
m_list = [Data.m];
i_list = [Data.i];
value = Data(m_list == 3 & i_list == 200).value;
Now you can run a loop easily over all data.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 10 Dec 2022
Edited: Image Analyst on 10 Dec 2022
Use the first snippet, not the second method like you're doing.
Avoid the problem of missing files by using dir() to get only files that actually exist.
% Specify the folder where the files live.
input_folder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(input_folder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
input_folder = uigetdir(); % Ask for a new one.
if input_folder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(input_folder, '*.txt'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as parsing it to get i and m.
% Parse 'CV_dm_diei.txt'
underlineLocations = strfind(baseFileName, '_');
m = str2double(baseFileName(underlineLocations(1) + 2 : underlineLocations(2) - 1));
dieIndex - strfind(baseFileName, '_die');
i = str2double(baseFileName(dieIndex + 4: end - 4));
% Read in the data (code below is not mine or from the FAQ).
Data(k).value = dlmread(fullFileName, ',', 4, 0);
Data(k).m = m;
Data(k).i = i;
end

Community Treasure Hunt

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

Start Hunting!