Error in reading dimensions of a .mat file

조회 수: 1 (최근 30일)
divya r
divya r 2012년 6월 17일
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

답변 (2개)

Walter Roberson
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.
  댓글 수: 1
divya r
divya r 2012년 6월 17일
I have extracted the features from a lot of images n stored it as row vectors in individual mat files. I am using euclidean distance method to find distance between each of them.
I have seen some programs which are working perfectly fine using the method i have used above. I dont understand why it isnt working here.
Should i not convert it into string? Else how will I read the files in an iterative manner?

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


Image Analyst
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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by