Error in reading dimensions of a .mat file
조회 수: 1 (최근 30일)
이전 댓글 표시
q is a matrix of 1*96. f_s is a matrix of 7*1. I am getting an error saying : index exceeds matrix dimensions.
And also the maximum value i = 1 and j=46 and e_dist matrix obtained is a 1*46 matrix. Expected values of i=7, j=96 and e_dist should be a 7*96 matrix.
f_s = 'C:\Users\Toshiba\Desktop\friday work\';
q = 'C:\Users\Toshiba\Desktop\friday work\1_1_2.mat';
s = dir(f_s);
s([s.isdir]) = [];
e_dist=[ ];
for i=1:1:size(s,1)
word=strcat(f_s, s(i).name);
for j=1:1:size(q,2);
e_dist(i,j)= sqrt(sum((word(i,j)-q(j)).^2));
end
end
댓글 수: 0
답변 (2개)
Walter Roberson
2012년 6월 17일
Your line
word=strcat(f_s, s(i).name);
creates "word" as a string, which is a row vector of characters. But then your line
e_dist(i,j)= sqrt(sum((word(i,j)-q(j)).^2))
attempts to access the i'th row of that character array; when "i" reaches 2, there will not be a 2nd row of the string and so you will get index exceeding matrix dimension.
Your "q" is not a matrix of 1*96: it is a character vector of 1*46 or so. Your "f_s" is not a matrix of 7*1: it is a character vector of 1*37 or so.
Your line
e_dist(i,j)= sqrt(sum((word(i,j)-q(j)).^2))
is calculating the Euclidean distance between file names.
It appears you have confused file names with the contents of the file.
Image Analyst
2012년 6월 17일
You need to call load(). Do it once for your reference mat file, then in the loop, do it for all the other files. Something like (untested)
folder = 'C:\Users\Toshiba\Desktop\friday work\';
refFileName = fullfile(folder, '1_1_2.mat');
if ~exist(refFileName, 'file')
errorMessage = sprintf('Error: reference file does not exist:\n%s', refFileName);
uiwait(warndlg(errorMessage ));
return;
end
% Get the reference array.
storedStructure = load(refFileName);
refImage = storedStructure.myImage; % or whatever.
filePattern = [folder, '*.mat'];
matFilenames = dir(filePattern);
% Loop over the other arrays and compare to the ref image.
for k = 1 : numberOfFiles
thisFileName = fullfile(folder, matFilenames(k).name);
storedStructure = load(thisFileName);
thisImage = storedStructure.myImage; % or whatever.
differenceImage = double(refImage) - double(thisImage);
rmsDifference(k) = sqrt(sum(differenceImage(:)));
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!