MATLAB Answers

how to select a row of data from a csv file that is imported into Matlab?

조회 수: 6(최근 30일)
I need to read in a csv file and utilize a function that is built into matlab:
[a,ecc,incl,RAAN,argp,nu,truelon,arglat,lonper] = ijk2keplerian(r_ijk, v_ijk)
Here is what my csv file looks like:
how should I import this csv file into matlab and then make a set of row vectors r = [rx,ry,rz] and v = [vx,vy,vz] such that I can use the above function directly?
I also want to create a plot of all six elements returned by the ijk2keplerian(r_ijk, v_ijk) function vs time. How should I make a for loop such that the function ijk2keplerian(r_ijk, v_ijk) returns all 6 elements into lists so I can plot each of the six elements vs time (on the same plot with legend)??

  댓글 수: 0

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

채택된 답변

Sai Veeramachaneni
Sai Veeramachaneni 17 Nov 2020
Here are the few hints.
  1. Use readtable function to read csv file into matlab as a table.
  2. Get the required column of the table using tablevariablename.columnname
Example
filename='file.csv';
table_data=readtable(filename);
r=[table_data.rx(1),table_data.ry(1),table_data.rz(1)];
v=[table_data.vx(1),table_data.vy(1),table_data.vz(1)];
[a,ecc,incl,RAAN,argp,nu,truelon,arglat,lonper] = ijk2keplerian(r, v)

  댓글 수: 4

표시 이전 댓글 수: 1
Sai Veeramachaneni
Sai Veeramachaneni 17 Nov 2020
You can iterate over each row of rx,ry,rz and vx,vy,vz using for loop
You can try something like below.
for i = 1:length(table_data.rx)
r=[table_data.rx(i),table_data.ry(i),table_data.rz(i)];
v=[table_data.vx(i),table_data.vy(i),table_data.vz(i)];
[a,ecc,incl,RAAN,argp,nu,truelon,arglat,lonper] = ijk2keplerian(r, v)
end
Randy Chen
Randy Chen 17 Nov 2020
i see, i have one last quesiton. So i want to create a plot of each of the elements[a,ecc,incl,RAAN,argp,nu,truelon,arglat,lonper] vs time given in the CSV. I just tried the for loop but it's not returning the values of [a,ecc,incl,RAAN,argp,nu,truelon,arglat,lonper] into a column vector for each element. How should I modify the codes so that I can get a plot of each of the 6 elements vs time? (6 lines on 1 graph with legend perhaps)
Sai Veeramachaneni
Sai Veeramachaneni 18 Nov 2020
You can build row vector inside for loop and later use this vector to plot the graphs.
See below code for your reference.
column_a=[]
column_ecc=[]
column_incl=[]
for i = 1:length(table_data.rx)
r=[table_data.rx(i),table_data.ry(i),table_data.rz(i)];
v=[table_data.vx(i),table_data.vy(i),table_data.vz(i)];
[a,ecc,incl,RAAN,argp,nu,truelon,arglat,lonper] = ijk2keplerian(r, v);
column_a=[column_a a]; %Storing value a inside row vector
column_ecc=[column_ecc ecc];
column_incl=[column_incl incl];
%Add remaining lines
end
plot(column_a,t)%t is the time
plot(column_ecc,t);
plot(column_incl,t);

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

추가 답변(0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by