Index Exceeds Number of Array Elements (1)? Line(5)

1 次查看(过去 30 天)
clear
close
clc
% Open and read formatted text file
fid=fopen('Lab1_Problem1.txt');
text=textscan(fid,'%s');
textcell=text{1};
% Need to convert the text string to a number
NumberofForces=str2double(textcell{8});
NumberofMoments= str2double(textcell(9));
% Preallocate ForceLocations
ForceLocations=zeros(NumberofForces.*3,1);
for i=25:(25+(NumberofForces*3)-1);
ForceLocations(i-24)=str2double(textcell(i));
end
ForceLocations=reshape(ForceLocations',[NumberofForces,3])'
% ForceMagnitudeAndDirection
x=37+(NumberofForces*3)
ForceMagnitudeAndDirection=zeros(NumberofForces*4,1);
for i=x:((NumberofForces*4)+x-1);
ForceMagnitudeAndDirection(i-(x-1))=str2double(textcell(i));
end
ForceMagnitudeAndDirection=reshape(ForceMagnitudeAndDirection,[4,NumberofForces])'
% Moment Locations
MomentLocations=zeros(NumberofForces,1);
y=13+((NumberofForces*4)+x)
for i=y:((NumberofMoments*3)+y-1)
MomentLocations(i-(y-1))=str2double(textcell(i));
end
MomentLocations=MomentLocations'
% MomentMagnitudeAndDirection
z=13+(NumberofMoments*3)+y
MomentMagnitudeAndDirection=zeros(NumberofForces+1,1);
for i=z:((NumberofMoments*4)+z-1);
MomentMagnitudeAndDirection(i-(z-1))=str2double(textcell(i));
end
MomentMagnitudeAndDirection=MomentMagnitudeAndDirection'
% LocationofSupports
t=8+(NumberofMoments*4)+z
LocationofSupports=zeros(1,18);
for i=t:(17+t)
LocationofSupports(i-(t-1))=str2double(textcell(i));
end
LocationofSupports=reshape(LocationofSupports,[3,6])'
% ReactionTypeDirection
ReactionTypeDirection=zeros(1,24);
m=30+t
for i=m:4:m+23
if textcell{i}=='M';
textcell{i}="0";
else
textcell{i}=='F';
textcell{i}="1";
end
end
ReactionTypeDirection=zeros(1,24);
for i=m:(23+m);
ReactionTypeDirection(i-(m-1))=str2double(textcell{i});
end
ReactionTypeDirection=reshape(ReactionTypeDirection,[4,6])'
  2 个评论
madhan ravi
madhan ravi 2019-9-16
编辑:madhan ravi 2019-9-16
How do you expect help without being able to run your code?
Lorenzo Madera
Lorenzo Madera 2019-9-16
It runs just fine in MATLAB however when I try to run it in an online grader it gives me and error saying that the index exceeds the number of array elements.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2019-9-16
Line 5 is
fid=fopen('Lab1_Problem1.txt');
In order to receive an index out of range in that line, you would have to have a variable named fopen in your workspace, making that into an indexing request instead of a function call.
Somehow I suspect that your actual line 5 is the one
NumberofForces=str2double(textcell{8});
If so then the difficulty is that your file Lab1_Problem1.txt has only 1 whitespace-delimited "words" in it.
  2 个评论
Lorenzo Madera
Lorenzo Madera 2019-9-16
Yes you are correct the error is coming from the NumberofForces line. However, what exactly do you mean by only 1 whitespace-delimited "words"?
Walter Roberson
Walter Roberson 2019-9-16
textscan with '%s' format is not going to read the file line by line. Instead, it is going to cycle around, reading character by character from the file and discarding everything it considers whitespace. When it finds a character that is not whitespace then the %s format tells it to read character by character. Each character that is not whitespace is put at the end of the current row of a cell array, making the output for that row one character longer. If it finds end of file while doing this, then it stops collecting input and returns from textscan. If it encounters a whitespace character then it will put the whitespace "back" ready to be read on the next read request, and finishes the current entry. Then it cycles back through, and the process of reading and discarding leading whitespace starts again, positioning to the first non-whitespace, start recording, etc..
Notice in this process, textscan makes no distinction between whitespace between characters in a sentence and newlines. It would treat
One Two
the same as
One
Two
The process is not line by line, it is sequences delimited by whitespace. This does not correspond to grammatical "words". For example, grammatically "This—and that—exist." has four words and three punctuation symbols, but textscan would break only at whitespace so it would get "This—and" and "that—exist."
At the time of the problem, your code expects that there will be at least 8 of these whitespace delimited sequences of characters, but your code is finding only 1.
I suggest that as a debugging step, you use fileread() on the file and dump out double() of the contents in order to find out exactly what characters are present.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by