Error in reading dimensions of a .mat file

1 view (last 30 days)
divya r
divya r on 17 Jun 2012
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

Answers (2)

Walter Roberson
Walter Roberson on 17 Jun 2012
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 Comment
divya r
divya r on 17 Jun 2012
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?

Sign in to comment.


Image Analyst
Image Analyst on 17 Jun 2012
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

Categories

Find more on Data Type Conversion 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!