64 views (last 30 days)
Show older comments
Alberto Acri on 21 Aug 2024 at 11:27
Answered: Madheswaran on 23 Aug 2024 at 5:51
- DATA.mat
Open in MATLAB Online
Hi. I have this file 'DATA'. The first 3 columns are the nodes in space (X,Y,Z in [m]). The 4th column is not of interest. The fifth column is the velocity in [m/s].
Is it possible to visualize the motion (green direction) of a sphere (e.g., the red one in the figure) moving following the velocity values in the fifth column of DATA?
load("DATA.mat");
figure
plot3(DATA(:,1),DATA(:,2),DATA(:,3),'-k','LineWidth',0.1);
hold on
plot3(DATA(1,1),DATA(1,2),DATA(1,3),'r.','MarkerSize',30);
hold off
axis equal
xlabel('x')
ylabel('y')
zlabel('z')
grid off
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (2)
Aquatris on 21 Aug 2024 at 12:04
Edited: Aquatris on 21 Aug 2024 at 12:09
Open in MATLAB Online
- data.mat
If you dont care about the actual timing and things, here is one way where the colors of the path indicate the speed and a simple animation show the full motion:
load('data.mat')
x = [DATA(:,1);nan];
y = [DATA(:,2);nan];
z = [DATA(:,3);nan];
v = [DATA(:,5);nan];
l = length(x);
figure(1)
for i = 1:1 % change it to 1:l to animate
clf
colormap(winter)
patch(x,y,z,v,'Facecolor','none','EdgeColor','interp')
colorbar
view(150,15)
grid on
grid minor
hold on
plot3(x(i),y(i),z(i),'ro','MarkerSize',15)
hold off
pause(0.1)
end
2 Comments Show NoneHide None
Show NoneHide None
Alberto Acri on 21 Aug 2024 at 13:11
Direct link to this comment
https://webchat.mathworks.com/matlabcentral/answers/2146869-generate-the-motion-trajectory-of-a-sphere-or-point-with-given-velocity-value#comment_3242809
Open in MATLAB Online
- newData_SEL_cell.mat
Thank you. It might be helpful. If I would like to display multiple 'DATA' matrices present in a cell 'newData_SEL_cell' how can I display the trends simultaneously in the same figure?
I've tried a for loop but it doesn't seem to work the way it's supposed to. Suggestion?
load('newData_SEL_cell.mat')
figure
for S = 1:height(newData_SEL_cell)
DATA = newData_SEL_cell(S,1); % 1 e 50
DATA = cell2mat(DATA);
x = [DATA(:,1);nan];
y = [DATA(:,2);nan];
z = [DATA(:,3);nan];
v = [DATA(:,5);nan];
l = length(x);
%figure
for i = 1:l % change it to 1:l to animate
clf
colormap(winter)
patch(x,y,z,v,'Facecolor','none','EdgeColor','interp')
colorbar
view(150,15)
grid off
%grid minor
hold on
plot3(x(i),y(i),z(i),'ko','MarkerSize',2)
plot3(x(i),y(i),z(i),'ko','MarkerSize',1)
plot3(x(i),y(i),z(i),'ko','MarkerSize',3)
hold off
axis equal
pause(0.1)
end
end
Aquatris on 21 Aug 2024 at 13:49
Direct link to this comment
https://webchat.mathworks.com/matlabcentral/answers/2146869-generate-the-motion-trajectory-of-a-sphere-or-point-with-given-velocity-value#comment_3242834
Edited: Aquatris on 21 Aug 2024 at 13:58
Open in MATLAB Online
- newData_SEL_cell.mat
Assuming each row happens at the same time:
load('newData_SEL_cell.mat')
% get x,y,z,v as cell arrays
x = cellfun(@(x) {[x(:,1);nan]},newData_SEL_cell);
y = cellfun(@(x) {[x(:,2);nan]},newData_SEL_cell);
z = cellfun(@(x) {[x(:,3);nan]},newData_SEL_cell);
v = cellfun(@(x) {[x(:,5);nan]},newData_SEL_cell);
% maximum number of data points in the whole data set
L = max(cellfun(@(A) length(A),x));
figure(1)
% loop through data points
for i = 1:1 % change it to 1:(L-1) to animate
clf
for j = 1:length(x) % loop through data sets
colormap(winter)
patch(x{j},y{j},z{j},v{j},'Facecolor','none','EdgeColor','interp')
colorbar
view(150,15)
grid on
grid minor
hold on
% because the 3 datasets are not the same length we need to check
if (length(x{j}) >= i) % check if we are not at the end point
plot3(x{j}(i),y{j}(i),z{j}(i),'ro','MarkerSize',15)
else
% (end-1) cause last element we put a 'nan' to prevent unnecessary
% connection between last point and first point during 'patch'
plot3(x{j}(end-1),y{j}(end-1),z{j}(end-1),'ro','MarkerSize',15)
end
end
hold off
pause(0.1)
end
Sign in to comment.
Madheswaran on 23 Aug 2024 at 5:51
Open in MATLAB Online
Hello,
The data appears somewhat unclear, as it specifies the object's velocity only at certain spatial positions, necessitating some assumptions. Here is the assumption made regarding the object's velocity in the intermediate position between the position mentioned in any two rows:
For the space between DATA(i, 1:3) and DATA(i+1, 1:3), the object is assumed to moving at a constant velocity as described in DATA(i, 5).
Below is a simple script to visualize the object's movement using a loop:
load DATA.mat
figure
plot3(DATA(:, 1), DATA(:, 2), DATA(:, 3));
% changing the aspect ratio and view to get the similar view as the posted image
pbaspect([1 1 4])
view([154.8 19.8])
ln = size(DATA, 1);
time_unit_divisor = 10;
hold on
pt = plot3(DATA(1, 1), DATA(1, 2), DATA(1, 3), 'r.', 'MarkerSize',30);
for i = 2:ln
distance = norm(DATA(i, 1:3) - DATA(i-1, 1:3));
velocity = DATA(i-1, 5);
time_taken = distance/velocity;
adjusted_time = time_taken/time_unit_divisor;
pause(adjusted_time);
%update the new position
pt.XData = DATA(i, 1);
pt.YData = DATA(i, 2);
pt.ZData = DATA(i, 3);
end
hold off
The above code would produce the plot like this:
You can adjust the speed of the animation by modifying the ‘time_unit_divisor’ variable. To enhance the smoothness of the animation, consider plotting additional points within each iteration by interpolating the intermediate positions of the object.
Hope this answers your question!
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office