How to save multiple .mat files data into single .mat file?

9 views (last 30 days)
i have multiple .mat files. Each file contain multiple image patches/blocks of size 32*32 of single image. All the mages are of different sizes. i want to copy all the data from multiple files into a single .mat file. Kindly help if somone knows the answer.

Answers (1)

Deepak
Deepak on 18 Oct 2024
From my understanding, you have multiple “.mat” files, each containing multiple image patches/blocks of size 32 x 32 and all the images are of different size. You want to concatenate entire “.mat” files patches data into a single “.mat” file.
To achieve this, we first need to load all the required “.mat” files into MATLAB. We can then iterate over each file to extract the “patches” data. After extracting the data, we can concatenate it into an output cell array. Finally, we can save the output cell array data into a new “.mat” file.
Here is the complete MATLAB code to accomplish this task:
% Directory containing the .mat files
folderPath = 'path_to_your_mat_files';
outputFile = 'combinedData.mat';
% Get a list of all .mat files in the directory
matFiles = dir(fullfile(folderPath, '*.mat'));
% Initialize a cell array to hold all patches
allPatches = {};
for k = 1:length(matFiles)
% Load the current .mat file
currentFile = fullfile(folderPath, matFiles(k).name);
data = load(currentFile);
% Assuming the variable containing patches is named 'patches'
if isfield(data, 'patches')
patches = data.patches;
else
error(['Variable "patches" not found in ', matFiles(k).name]);
end
% Concatenate patches into the cell array
allPatches = [allPatches; patches]; % Append patches to the cell array
end
save(outputFile, 'allPatches');
Please find attached the documentation of functions used for reference:
I hope this assists in resolving the issue.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!