how to extract one variable from mat flies

조회 수: 7 (최근 30일)
Pengju
Pengju 2014년 9월 10일
댓글: Image Analyst 2014년 9월 12일
Dear all, I have one sequence of mat files (1,2, ... 100.mat), each mat file as below: Name Size Bytes Class Attributes
c 1x1 552 struct
data 500x500 2000000 double
timestamp 1x55 110 char
now, I just need the data, would you please tell me how to extract the data from all of the mat files?
Looking forwards your kind help.
Pengju

채택된 답변

Image Analyst
Image Analyst 2014년 9월 12일
편집: Image Analyst 2014년 9월 12일
myFolder = 'D:\myData';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get list of all .mat files in this folder.
filePattern = fullfile(myFolder, '*.mat');
matFiles = dir(filePattern);
% Go through all mat files, extracting "data" and summing it
% into an accumulation array.
for k = 1:length(matFiles)
baseFileName = matFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
thisData = load(fullFileName);
if k == 1
% Initialize with the first array.
sumOfAllData = thisData.data;
else
% Assume all data arrays are the same size and single or double, not uint8.
% add them together.
sumOfAllData = sumOfAllData + thisData.data;
end
end
  댓글 수: 5
Pengju
Pengju 2014년 9월 12일
aha, it works.... thanks a lot!
Image Analyst
Image Analyst 2014년 9월 12일
Yeah, sorry I forgot to tell you that if you don't accept the output of load() into a variable it "poofs" variables into existence. Generally people don't like that method. If you accept the output into a variable, the variable is a structure with all the internal variables as members/fields of that structure.

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

추가 답변 (1개)

Robert Cumming
Robert Cumming 2014년 9월 10일
You can request the "data" variable explicitly from the file:
load ( filename, 'data' );
Note this will add a new variable "data" to the workspace, so if you load 2 files the second load will overwrite the first, to load multiple files you could do:
files = dir ( '*.mat' );
for ii=1:length(files)
fileData{ii} = load ( files(ii).name, 'data' );
end
If all the "data" are the same size you could save it in a 3D matrix (pre-allocate first).
myMatrix = nan(500,50,100); % pre-allocate with NaN
for ii=1:length(files)
load ( files(ii).name, 'data' );
if exist ( 'data', 'var' ) % this checks to see if data was loaded from the load command.
myMatrix(:,:,ii) = data;
end
clear data % to avoid it being used in the next loop...
end
  댓글 수: 3
Robert Cumming
Robert Cumming 2014년 9월 11일
How do you plan to put 100 (variable "data") images onto a single image?
Pengju
Pengju 2014년 9월 11일
I have 100 mat files, each of them has data, which is matrix, this matrix represents one image, every image is independent to the other.
so, I need to accumulate the all the matrix to one matrix, then I load this matrix to plot it as image...

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

카테고리

Help CenterFile Exchange에서 MATLAB Report Generator에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by