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!