- 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
- Each .mat file has an array corresponding to each field. i.e. one for Name, one for ECG, with index same for each "person"
- Either of the above, with a different format than .mat
Merge multiple files containing given string into one mat file
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Elzbieta
am 15 Jun. 2024
Beantwortet: Elzbieta
am 19 Jun. 2024
Hello,
How to merge multiple files which names contains given strings (for instance: Name, ECG, data) into one file?
2 Kommentare
Ganesh
am 15 Jun. 2024
Bearbeitet: Ganesh
am 15 Jun. 2024
I'm sorry, but I am not able to understand the file structure.
Could you please confirm which one of these describes your data
DGM
am 15 Jun. 2024
Bearbeitet: DGM
am 15 Jun. 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".
Akzeptierte Antwort
Weitere Antworten (1)
Nipun
am 17 Jun. 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
- 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.
- Listing Files: The dir function lists all .mat files in the specified directory.
- 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.
- 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:
- dir: https://www.mathworks.com/help/matlab/ref/dir.html
- save: https://www.mathworks.com/help/matlab/ref/save.html
Hope this helps.
Regards,
Nipun
1 Kommentar
DGM
am 17 Jun. 2024
OP never said the files were .mat files.
The only information we have is that:
- the file names contain metadata
- 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.
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!