Real time update on 3D plot with GUI

9 views (last 30 days)
Dan Fitzgerald
Dan Fitzgerald on 25 Apr 2014
I am trying to make a GUI that will show a number of particles moving in 3D space and allow the user to control some of their parameters, while plotting other properties, all in real time. I've made a GUI using GUIDE with a 3D scatterplot of the particles, and a timer to update them which can be started and stopped, but those changes aren't being reflected in the plot. I'm storing the particles as part of the guidata handles structure, and I'm guessing my problem either has something to do with updating that structure or refreshing the 3D plot.
I haven't done anything like this in Matlab, so I've been going by code examples. Any additional suggestions for this type of app would be much appreciated!
This is the opening callback for the gui that set up the timer and the particles (which are just 3d positions and velocities for now.)
% --- Executes just before RealTimePlotter is made visible.
function RealTimePlotter_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to RealTimePlotter (see VARARGIN)
% Choose default command line output for RealTimePlotter
handles.output = hObject;
% Update handles structure
%guidata(hObject, handles);
% UIWAIT makes RealTimePlotter wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% START USER CODE
% Create a timer object to fire at 1/10 sec intervals
% Specify function handles for its start and run callbacks
handles.timer = timer(...
'ExecutionMode', 'fixedRate', ... % Run timer repeatedly
'Period', 1, ... % Initial period is 1 sec.
'TimerFcn', {@update_display,hObject}); % Specify callback function
% Initialize slider and its readout text field
set(handles.periodsldr,'Min',0.01,'Max',2)
set(handles.periodsldr,'Value',get(handles.timer,'Period'))
set(handles.slidervalue,'String',num2str(get(handles.periodsldr,'Value')))
global time;
time=0;
% Create a surface plot of peaks data. Store handle to it.
%handles.surf = surf(handles.simDisplay,peaks);
%%bounding box
handles.containerSize=100.0;
box=unique([-1 -1 -1; perms([1 1 -1]); perms([1 -1 -1]); 1 1 1], 'rows');
box=box.*handles.containerSize;
handles.container=box; %[0,handles.containerSize,0,handles.containerSize,0,handles.containerSize,0],Filled:FALSE, LineColor:Red);
%%particles
handles.nParticles=1000;
handles.temperature=2;
handles.particles = handles.containerSize*rand(handles.nParticles,3);
handles.particleVelocities=handles.temperature*rand(handles.nParticles,3);
%disp(handles.particles); %print particles
%%initial plotting/viewport settings
axis(handles.simDisplay);
% END USER CODE
% Update handles structure
guidata(hObject,handles);
This is the update function called by the timer which is supposed to move the particles and re-plot them.
%timer update
function update_display(hObject,eventdata,hfigure)
% Timer timer1 callback, called each time timer iterates.
% Gets surface Z data, adds noise, and writes it back to surface object.
disp('UPDATING...');
global time;
time=time+1;
disp(time);
handles = guidata(hfigure);
%Z = get(handles.surf, 'ZData');
%Z = Z + 0.1*randn(size(Z));
%set(handles.surf,'ZData',Z);
%%add particle velocities to the positions each time step
handles.particles=handles.particles+handles.particleVelocities;
%set(handles.particles, handles.particles+handles.particleVelocities);
%%plot particles
subplot(handles.simDisplay);
scatter3(rand(100,1),rand(100,1),rand(100,1), 'fill');
%drawContainer(hfigure);
%disp(handles.particles);
%view(50,50,50);
subplot(handles.tempPlotAxis);
plot([0 .5],[0 1]);
%subplot(handles.);
% Update handles structure
guidata(hfigure,handles);
refreshdata(handles.simDisplay);
drawnow;
% END USER CODE
The rest of the code is identical to that found in the ex_guide_timergui example.
Is there a better way of going about this? Thanks for any help.

Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!