필터 지우기
필터 지우기

How to copy data from multiple .mat files into single .mat file?

조회 수: 7 (최근 30일)
hirra awan
hirra awan 2020년 7월 17일
답변: Sindar 2020년 7월 17일
I have multiple .mat files which contain pixel values of images. each mat file has different number of data.. how can i copy or load all of data from multiple .mat files into a single .mat file?
  댓글 수: 1
Dana
Dana 2020년 7월 17일
The easiest way to do this depends on how many mat files there are, how many variables are in each mat file, whether you know the variable names in each file already, and whether variables in different mat files have the same name. Can you elaborate on your problem a bit more?

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

답변 (1개)

Sindar
Sindar 2020년 7월 17일
Assuming absolutely nothing about your data, this should dump the saved data into a single matfile (one variable per loaded file)
% list files to draw from
matfile_names = {'mat1.mat';'matb.mat';'frog'};
% remove extension, clear duplicates
matfile_names_stripped = unique(strrep(matfile_names,'.mat',''));
% create valid-variable-name versions
matfile_names_valid_variable = genvarname(matfile_names_stripped);
% prepare an empty structure
all_data = struct();
% for each file...
for ind=1:length(matfile_names_stripped)
% load the data into a field of all_data corresponding to the filename
try
all_data.(matfile_names_valid_variable{ind}) = load([matfile_names_stripped '.mat']);
catch ME
all_data.(matfile_names_valid_variable{ind}) = struct();
end
end
% save the fields of all_data to a new matfile
save('all_data.mat','-struct','all_data');
% version for larger data
% save('all_data.mat','-struct','all_data','-v7.3');

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by