Merge multiple files containing given string into one mat file

조회 수: 4 (최근 30일)
Elzbieta
Elzbieta 2024년 6월 15일
답변: Elzbieta 2024년 6월 19일
Hello,
How to merge multiple files which names contains given strings (for instance: Name, ECG, data) into one file?
  댓글 수: 2
Ganesh
Ganesh 2024년 6월 15일
편집: Ganesh 2024년 6월 15일
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 2024년 6월 15일
편집: DGM 2024년 6월 15일
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".

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

채택된 답변

Elzbieta
Elzbieta 2024년 6월 19일
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

추가 답변 (1개)

Nipun
Nipun 2024년 6월 17일
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
DGM
DGM 2024년 6월 17일
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.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by