"Index exceeds the number of array elements (1)."

조회 수: 8 (최근 30일)
Jessica Larry
Jessica Larry 2020년 4월 28일
편집: Toder 2020년 4월 29일
load('air.txt')
Air.temp = air(:,1);
Air.press = air(:,2);
Air_R = 0.287;
load('argon.txt')
Argon.press = argon(:,2);
Argon.temp = argon(:,1);
Argon_R = 0.208;
load('helium.txt')
Helium.press = helium(:,2);
Helium.temp = helium(:,1);
Helium_R = 2.080;
for k = 1:length(air)
Air(k).vol = (Air_R*Air(k).temp)./Air(k).press;
end
for k = 1:length(argon)
Argon(k).vol = (Argon_R*Argon(k).temp)./Argon(k).press;
end
for k = 1:length(helium)
Helium(k).vol = (Helium_R*Helium(k).temp)./Helium(k).press;
end
Using the ideal gas law to caululate the velocity of argon, helium and air, but I keep getting and Error.
"Index exceeds the number of array elements (1)."
When I run the code through I only get the volume of air butnot the volumes of argon or helium? I tried to also create a auser defined function, but I wasn't able to figure out what was wrong with that either.
  댓글 수: 3
Toder
Toder 2020년 4월 29일
The error indicates that an index is asking for an element of an array where the element doesn't exist. For example, for x=[11,12,13], running x(2) will return 12, but x(4) will throw a similar error as yours because there is no fourth element in x. This is happening to one of your variables, but it's hard to tell which because we don't know the contents of air.txt, argon.txt, and helium.txt. However the error tells us that the array you're referring to has only one element.
What line throws the error (the error prints with a line number)? Can you give a description of the text files, or the files themselves if they aren't too large?
Jessica Larry
Jessica Larry 2020년 4월 29일
It says there's an error in line 31. Iv'e attached the 3 text files, and in the code you can see that i set each colum to a different field within a structure. Could it possibly be becuase Air is a 1X1 strcut?

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

채택된 답변

Toder
Toder 2020년 4월 29일
The way you define your struct in the first few lines results in Air being a 1x1 struct with two fields (temp and press), each of which is 38x1. So calling Air(2).temp throws an error because Air is only 1x1. To use the indices correctly, your first for loop should read
for k = 1:length(air)
Air.vol(k) = (Air_R*Air.temp(k))./Air.press(k);
end
  댓글 수: 2
Jessica Larry
Jessica Larry 2020년 4월 29일
Yes, that was the issue! Thanks!
Toder
Toder 2020년 4월 29일
편집: Toder 2020년 4월 29일
Glad I could help.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by