Merge multiple files containing given string into one mat file

3 vues (au cours des 30 derniers jours)
Elzbieta
Elzbieta le 15 Juin 2024
Hello,
How to merge multiple files which names contains given strings (for instance: Name, ECG, data) into one file?
  2 commentaires
Ganesh
Ganesh le 15 Juin 2024
Modifié(e) : Ganesh le 15 Juin 2024
I'm sorry, but I am not able to understand the file structure.
  1. Each file is a .mat file which contains data of different people. i.e. Same fields, different entries, and you need to combine them into an array
  2. Each .mat file has an array corresponding to each field. i.e. one for Name, one for ECG, with index same for each "person"
  3. Either of the above, with a different format than .mat
Could you please confirm which one of these describes your data
DGM
DGM le 15 Juin 2024
Modifié(e) : DGM le 15 Juin 2024
You've only told us that the filenames vary somehow. You haven't said how the filenames vary, what the file type is, what the files contain, or how their contents should be combined.
Until you attach example files, or otherwise adequately describe what you have and what you want, the answer is "it depends".

Connectez-vous pour commenter.

Réponse acceptée

Elzbieta
Elzbieta le 19 Juin 2024
Hello,
Finally I figured out the following code:
names = {'Alessandra', 'Alfredo', 'Carla', 'Giulia', 'Ilaria', 'Letizia', 'Lucrezia', 'Mariangela', 'Martina', 'Michela'}
% Get a list of all files in the folder with the desired file name pattern.
for i = 1: length(names)
filePattern = fullfile(myFolder,['*',names{i},'*trial*ECG*data.mat']) % Change to whatever pattern you need.
theFiles = dir(filePattern)
data = [];
for k = 1 : length(theFiles)
new_data = [];
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName)
display("new_data")
new_data = load(fullFileName, '-ascii');
size(new_data)
if (isrow(new_data) == 1)
data = [data; new_data'];
else
data = [data; new_data];
end
size(data)
data;
end
outFullFileName = fullfile(myFolder,[names{i},'_trial_ECG_data.txt'])
save(outFullFileName, 'data', '-tabs', '-ascii')
end

Plus de réponses (1)

Nipun
Nipun le 17 Juin 2024
Hi Elzbieta,
I understand that you want to merge multiple .mat files whose names contain specific strings (e.g., "Name", "ECG", "data") into a single .mat file. Below is the MATLAB code to achieve this:
function merge_files_to_matfile(directory, search_strings, output_filename)
% List all .mat files in the specified directory
files = dir(fullfile(directory, '*.mat'));
% Initialize a structure to store the merged data
merged_data = struct();
% Loop through each file and check if it contains any of the search strings
for file = files'
for str = search_strings
if contains(file.name, str{1})
% Load the data from the file
file_data = load(fullfile(directory, file.name));
% Merge the data into the merged_data structure
fields = fieldnames(file_data);
for i = 1:length(fields)
field = fields{i};
if isfield(merged_data, field)
merged_data.(field) = [merged_data.(field); file_data.(field)];
else
merged_data.(field) = file_data.(field);
end
end
break; % No need to check other strings if one matches
end
end
end
% Save the merged data to the specified output file
save(output_filename, '-struct', 'merged_data');
end
% Example usage:
directory = 'path_to_your_directory';
search_strings = {'Name', 'ECG', 'data'};
output_filename = 'merged_data.mat';
merge_files_to_matfile(directory, search_strings, output_filename);
Explanation
  1. Function Definition: The merge_files_to_matfile function takes three arguments:
  • directory: The directory containing the .mat files.
  • search_strings: A cell array of strings to search for in the filenames.
  • output_filename: The name of the output .mat file.
  1. Listing Files: The dir function lists all .mat files in the specified directory.
  2. File Matching and Merging: The function checks if each file's name contains any of the search strings. If it does, it loads the file and merges its contents into a structure merged_data.
  3. Saving Merged Data: The merged data is saved into a single .mat file with the specified output filename.
For more information on the dir and save functions, you can refer to the MathWorks documentation:
Hope this helps.
Regards,
Nipun
  1 commentaire
DGM
DGM le 17 Juin 2024
OP never said the files were .mat files.
The only information we have is that:
  1. the file names contain metadata
  2. the contents of these unknown files should be crammed into a .mat file
We don't know what the filename format is
We don't know what the file type is
We don't know what the file contents are
We don't know how the contents should be merged.
I usually wait till a question is dead and cold before I assert a blind interpretation, but I guess "dead and cold" is fair to interpret at this point.

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by