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 Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by