getting "Index exceeds matrix dimensions." Can anyone explain?

2 ビュー (過去 30 日間)
siddhesh rane
siddhesh rane 2013 年 5 月 21 日
コメント済み: DGM 2025 年 10 月 8 日
function [ coordinates ] = readSTL1( filename,k)
% reads ASCII STL file and gives coordinates of vertices.
%filename-name of file(test.txt),k- number of rows in cell.
fid=fopen(filename);
C=textscan(fid,'%s');
%reads file and generates cell.
m = 11;
i = 1;
coordinates = zeros(4455,1);
while(m < (k-3))
j = 1;
while (j < 4)
l = 1;
while(l<4)
coordinates(i) = C(m);
l = l+1; % makes sure loop runs thrice.
m = m+1; % access corresponding row from cell'C'.
i = i+1; % row number in output matrix.
end
m = m+1;
j = j+1;
end
m = m+10;
end
end
  2 件のコメント
siddhesh rane
siddhesh rane 2013 年 5 月 21 日
error message-
Index exceeds matrix dimensions.
Error in readSTL1 (line 15) coordinates(i) = C(m);
DGM
DGM 2025 年 10 月 8 日
Besides the basic handling of cell arrays and cellchar data, this magic number:
m = 11;
... presumes that the file's header text contains exactly one whitespace-delineated blob. It will result in completely useless jumbled garbage if the header text is empty, or contains multiple words or numbers.
Likewise, the method of blindly extracting a rigidly ordered pattern of blobs from the text will produce similar jumbled garbage if there's any variation in the file structure, such as in a multiobject ASCII STL.
Also, close the file.
At the time, there were multiple ASCII STL decoders on the FEX already.

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 5 月 21 日
Are you sure that is your code? textscan() returns a cell array, so your C is a cell array and thus C(m) is a 1x1 cell array (which has a string inside it), but you cannot assign a cell array into a numeric array (you initialized coordinates as zeros() which is numeric).
Even if you were to change it to C{m} then C{m} would be a string, most likely of multiple characters, and assigning multiple characters into a single numeric location is not going to work.
When you use textscan with a '%s' format, the input will be divided up into (whitespace delimited) strings with line boundaries treated the same as whitespace, so C will contain one location per string. e.g.,
fizz 19 39
bar
would return
{'fizz'; '19'; '39'; 'bar'}
You assume in your code that m starts from 11, so your code that accesses C(m) is going to fail of there were not at least 11 strings in the file.
  2 件のコメント
siddhesh rane
siddhesh rane 2013 年 5 月 21 日
編集済み: siddhesh rane 2013 年 5 月 21 日
Yes..the cell has both numerical and text datas..i wrote this code to separate numerical data from text..numerical data repeats itself in certain sequence.so i have to copy data from those rows and create coordinates matrix .rowise sequence of occurance of data is {[(11,12,13)(15,16,17)(19,20,21)][(32,33,34)(36,37,38)(40,41,42)]}..for accessing thows rows i used three while loops..And as my cell will have atleast 11 columns i guess that wont be a problem.
Walter Roberson
Walter Roberson 2013 年 5 月 21 日
At the command line, give the command
dbstop if error
then run the program. When it stops at the error you indicated, examine size(C) and the value of m .

サインインしてコメントする。


Iain
Iain 2013 年 5 月 21 日
If you know the indices of where the data is, you can instead do:
Indices = [ 11 12 13 15 16 17 19 20 21 32 33 34 36 37 38 40 41 42];
for i = 1:numel(Indices)
coordinates(i) = C{Indices(i)};
end
The error occurs because your "m" is larger than the number of elements in C.

カテゴリ

Help Center および File ExchangeVector Volume Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by