Data extraction and indexing of array

3 次查看(过去 30 天)
Hi,
I have a structure of 1x5 and I want to extract X and Y position data of each individual in a single variables like posxdata, posydata, velxdata, velydata, against nid. The nid can range from 1 to 5000 depending upon the time duration.
Since here all the nid [1 to 5] has x and y values of 1x51. Kindly let me know if they have different values like
nid(1) =length(x) = 51 nid(1) =length(y) = 51
nid(2) =length(x) = 43 nid(2) =length(y) = 43
nid(3) =length(x) = 29 nid(3) =length(y) = 29
nid(4) =length(x) = 91 nid(4) =length(y) = 91
nid(5) =length(x) = 81 nid(5) =length(y) = 81
I have written this simple program to extract data. But I am facing dimension mismatch issue.
nid = 5;
xposdata = np.x;
yposdata = np.y;
for i= 1:nid
for j = 1: length(xposdata|yposdata)
xposdata(i,j) =np(i).x(1:end)
yposdata(i,j) =np(i).y(1:end)
velxposdata(i,j) =np(i).vx(1:end)
velyposdata(i,j) =np(i).vy(1:end)
end
end
nid = 5;
xposdata = np.x;
yposdata = np.y;
velxposdata= np.v_x;
velyposdata= np.v_y;
for i= 1:nid
for j = 1: length(xposdata|yposdata)
xposdata(i,j:end) =np(i).x(1:end);
yposdata(i,j:end) =np(i).y(1:end);
velxposdata(i,j:end) =np(i).v_x(1:end);
velyposdata(i,j:end) =np(i).v_y(1:end);
end
end
I need your guideline and recommendation of some short course which can develop understanding of data extraction and manipulation. Mat file is attached thanks
Thanks

采纳的回答

Sudheer Bhimireddy
If your data size changes throughout the structure then load them into a cell array and access each entry.
nid = 5;
xposdata = np.x;
yposdata = np.y;
velxposdata= np.v_x;
velyposdata= np.v_y;
for i= 1:nid
xposdata{i} = np(i).x(1:end);
yposdata{i} = np(i).y(1:end);
velxposdata{i} = np(i).v_x(1:end);
velyposdata{i} = np(i).v_y(1:end);
end
% for plotting them
h=figure;
clf;
hold on;
for i =1:nid
x = xposdata{i};
y = yposdata{i};
plot(x,y);
end
  2 个评论
Arsal15
Arsal15 2020-8-7
Hi Sudheer Bhimireddy,
I tried this solution and error is same. I have attached new NP.mat with x,y data has different length.
Sudheer Bhimireddy
My bad. Before the for loop the xposdata and similar variables are already initialized.
Change them to:
xposdata = cell(nid,1);
yposdata = cell(nid,1);
velxposdata = cell(nid,1);
velyposdata = cell(nid,1);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by