How to reference a variable saved as .mat file to execute code

조회 수: 3 (최근 30일)
JD
JD 2019년 9월 18일
댓글: Walter Roberson 2019년 9월 18일
So I have a variable named cyc_mph that is a 1370x2 double. This data is stored as a .mat file.
The first column in the variable file gives me 't' (which goes from 0 to 1369) and the second column gives me 'Vmph'
I am trying to calculate 'a' by the formula given below. But what I want the code to do, is to get the value of 'Vmph' that corresponds to its 't+1' or 't-1' in the data file and give me 'a' for t=2:1:1369.
I do not think my code is gathering the correct 'Vmph' corresponding to the correct 't+1' or 't-1' in my code below. Do you know what I am doing wrong?
CODE:
t = cyc_mph(:,1);
Vmph = cyc_mph(:,2);
for t=2:1:1369
a = (Vmph(t+1)-Vmph(t-1))/(2*((t+1)-(t-1)));
A = ((1/2)*Rhoa*Cd*Af*(Vmph(t).^3));
G = (Mv*g*cos(0).*Vmph(t));
I = (1.1*Mv.*a.*Vmph(t));
Pw(t-1) = (A+G+I)/1000;
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 9월 18일
t = cyc_mph(:,1);
That extracts t values from the array.
for t=2:1:1369
That overwrites that vector of t values with integer constants 2, 3, up to 1369, one at a time.

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

채택된 답변

Walter Roberson
Walter Roberson 2019년 9월 18일
t = cyc_mph(:,1);
Vmph = cyc_mph(:,2);
for tidx=2:length(t)-1
a = (Vmph(tidx+1)-Vmph(tidx-1))/(2*(t(tidx+1)-t(tidx-1)));
A = ((1/2)*Rhoa*Cd*Af*(Vmph(tidx).^3));
G = (Mv*g*cos(0).*Vmph(tidx));
I = (1.1*Mv.*a.*Vmph(tidx));
Pw(tidx-1) = (A+G+I)/1000;
end
  댓글 수: 2
JD
JD 2019년 9월 18일
Thank you Walter! If you don't mind, can you please explain the logic behind your code so I can understand for future reference?
Walter Roberson
Walter Roberson 2019년 9월 18일
You had the statement
t = cyc_mph(:,1);
so in one place you are expecting t to refer to some input data . But right after you had
for t=2:1:1369
so your t now referring to datapoint numbers instead.
From there I traced the logic of the code and figured out which references to t were likely to be datapoint numbers and which ones were likely to need the input times, and then I changed the datapoint number version to variable named tidx and changed the input times to t(tidx) to refer to the current input data value.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by