How to plot x-z plot which is varying in time (a dynamic plot)?

I have my data (attached here), with col1: ion number, col2:x position, col3: yposition, col4: z position, col5: time, col6: temperature. I want to plot x-z (then later y z and temperature,z etc separately) which is varing in time according to column 4. The data points need to be dark colored dots to have a better view as if it looks like particles moving with time when I playthe time bar. As the x, y and z recorded each 500 microseconds every time, is it possible to plot with ta time bar so that I can drag the bar and show at each time what exactly the value of x and z? Can anyone help me to make a code for this? I dont have additional toolboxes! hope I can make with basic matlab codes.

 Respuesta aceptada

Voss
Voss el 10 de Nov. de 2022
Editada: Voss el 14 de Nov. de 2022
Something like this might be a start, if I understand what you want to do:
function ionGUI()
t = readtable('SampleData.txt');
[time,~,jj] = unique(t.time);
f = figure();
ax = [];
ax(end+1) = subplot(3,1,1);
title('x-z');
ax(end+1) = subplot(3,1,2);
title('y-z');
ax(end+1) = subplot(3,1,3);
title('temp-z');
ion_line = [ ...
line( ...
'Parent',ax(1), ...
'LineStyle','none', ...
'Marker','.', ...
'XData',[], ...
'YData',[]);
line( ...
'Parent',ax(2), ...
'LineStyle','none', ...
'Marker','.', ...
'XData',[], ...
'YData',[]);
line( ...
'Parent',ax(3), ...
'LineStyle','none', ...
'Marker','.', ...
'XData',[], ...
'YData',[]) ...
];
slider = uicontrol( ...
'Parent',f, ...
'Style','slider', ...
'Units','normalized', ...
'Position',[0.05 0 0.9 0.05], ...
'Value',0, ...
'SliderStep',[1 10]/(numel(time)-1), ...
'Callback',@cb_slider);
cb_slider(slider);
function cb_slider(src,~)
idx = 1+round(get(src,'Value')*(numel(time)-1));
z = t{jj == idx,'z'};
set(ion_line(1),'XData',z,'YData',t{jj == idx,'x'});
set(ion_line(2),'XData',z,'YData',t{jj == idx,'y'});
set(ion_line(3),'XData',z,'YData',t{jj == idx,'temperature'});
end
end
Alternatively, you might look into using animated lines.

4 comentarios

Super!!!! Excellent! this is what I wanted.. Just a few questions too.. how can I get the time displayed somewhere near the bar, so that I know what time it is now displaying? How can I display different color for different masses. Currently in the mass column there most of them are 768.9 and a few are 645.33.. In my other data I have much different masses. Thanks a lot...
I tried to add this code
[mass,~,~] = unique(t.mass);
if t.mass ==354.19
co = 'g';
elseif t.mass==645.33
co ='m';
elseif t.mass==768.9
co ='b';
else
co = 'k';
end
The within ion_line
line( ...
'Parent',ax(1), ...
'LineStyle','none', ...
'Marker','o', ...
'MarkerFaceColor', co, ...
'XData',[], ...
'YData',[]);
But it take only 'k'.... The table already have the other mass values but it is not taking co values according to the for loop. What could be the problem?
Thank you.
Another attempt I made was to use this function in a different script with for loop plotting as follows, still no success :(
mlist =[354.19 645.33 768.9] ;
col = ['b' 'm' 'g'];
t1 = readtable('SampleData.txt');
for k = 1:size(mlist,2)
m = mlist(1,k);
rows = (t1.mass == m);
t =t1(rows,:);
c=col(1,k);
ionGUI(t,c)
hold on
end
hold off
Here I m calling the function separately for each mass and plottng on the same figure... I am not getting why this is not working!
Voss
Voss el 14 de Nov. de 2022
Editada: Voss el 14 de Nov. de 2022
Maybe something like this:
function ionGUI()
t = readtable('SampleData.txt');
[mass,~,kk] = unique(t.mass);
[time,~,jj] = unique(t.time);
NM = numel(mass);
NT = numel(time);
f = figure();
ax = [];
ax(end+1) = subplot(3,1,1);
title('x-z');
ax(end+1) = subplot(3,1,2);
title('y-z');
ax(end+1) = subplot(3,1,3);
title('temp-z');
colors = get(ax(1),'ColorOrder');
NC = size(colors,1);
colors = repmat(colors,ceil(NM/NC),1);
ion_line = zeros(NM,3);
for m = 1:NM
for aa = 1:3
ion_line(m,aa) = line( ...
'Parent',ax(aa), ...
'LineStyle','none', ...
'Marker','.', ...
'MarkerEdgeColor',colors(m,:), ...
'MarkerFaceColor',colors(m,:), ...
'XData',[], ...
'YData',[]);
end
end
slider = uicontrol( ...
'Parent',f, ...
'Style','slider', ...
'Units','normalized', ...
'Position',[0.05 0 0.9 0.025], ...
'Value',0, ...
'SliderStep',[1 10]/(NT-1), ...
'Callback',@cb_slider);
t_obj = uicontrol( ...
'Parent',f, ...
'Style','text', ...
'Units','normalized', ...
'Position',[0.4 0.025 0.2 0.04], ...
'String','');
cb_slider(slider);
function cb_slider(src,~)
t_idx = 1+round(get(src,'Value')*(NT-1));
for mm = 1:NM
idx = jj == t_idx & kk == mm;
z = t{idx,'z'};
set(ion_line(mm,1),'XData',z,'YData',t{idx,'x'});
set(ion_line(mm,2),'XData',z,'YData',t{idx,'y'});
set(ion_line(mm,3),'XData',z,'YData',t{idx,'temperature'});
end
set(t_obj,'String',sprintf('time = %g',time(t_idx)));
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Preguntada:

el 10 de Nov. de 2022

Editada:

el 14 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by