Clear Filters
Clear Filters

Why doesn't this code continue to run the timerfunction?

2 views (last 30 days)
Hi! I made a code to simulate the boid's behaviors; I made a model to learn an Agent-based-model. But, I do not know why it dose not keep running the timerfcn. On the first try, I think it does work. I guess on the second try, there are some problem.
I got the following error message. Error while evaluating TimerFcn for timer 'timer-1'
Cell contents reference from a non-cell array object.
I tried to figure it out by myself; however, I did not know what is the problem.
Is there anyone who helps me?
Please.... I really want to know what is the problem with my code.
My code file is following: function varargout = boid_ex(varargin) % BOID_EX MATLAB code for boid_ex.fig % BOID_EX, by itself, creates a new BOID_EX or raises the existing % singleton*. % % H = BOID_EX returns the handle to a new BOID_EX or the handle to % the existing singleton*. % % BOID_EX('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in BOID_EX.M with the given input arguments. % % BOID_EX('Property','Value',...) creates a new BOID_EX or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before boid_ex_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to boid_ex_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help boid_ex
% Last Modified by GUIDE v2.5 01-Apr-2014 17:16:24
%%% Make changes needed for proper look and feel and running on %%%different platforms %%prepareLayout(boids); %%allObjects = findall(topContainer); %%% Make GUI objects available to callbacks so that they cannot %%% be changed accidentally by other MATLAB commands %% set(allObjects(isprop(allObjects,'HandleVisibility')),... %% 'HandleVisibility','Callback');
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @boid_ex_OpeningFcn, ... 'gui_OutputFcn', @boid_ex_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT
% --- Executes just before boid_ex is made visible. function boid_ex_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 boid_ex (see VARARGIN)
% Choose default command line output for boid_ex handles.output = hObject;
handles.N = 20; % define the number of boids % initialize boids position and velocity boid.pos = 5000.*rand(handles.N,2); boid.vel = [150.*rand(handles.N,1) 10.*rand(handles.N,1)]; boid_plotter(boid, handles) % call the function to plot the boids in the GUI window % create timer object handles.timer = timer(... 'TimerFcn', {@my_callback_fun,handles},'Period',.1, ... 'ExecutionMode','fixedRate','UserData',boid);
% Update handles structure guidata(hObject, handles);
% UIWAIT makes boid_ex wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = boid_ex_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure varargout{1} = handles.output;
% --- Executes on slider movement. function slider1_Callback(hObject, eventdata, handles) % hObject handle to slider1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties. function slider1_CreateFcn(hObject, eventdata, handles) % hObject handle to slider1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end
% --- Executes on slider movement. function slider2_Callback(hObject, eventdata, handles) % hObject handle to slider2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties. function slider2_CreateFcn(hObject, eventdata, handles) % hObject handle to slider2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end
% --- Executes on slider movement. function slider3_Callback(hObject, eventdata, handles) % hObject handle to slider3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties. function slider3_CreateFcn(hObject, eventdata, handles) % hObject handle to slider3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end
% --- Executes on button press in start. function start_Callback(hObject, eventdata, handles) % hObject handle to start (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) start(handles.timer);
% --- Executes on button press in stop. function stop_Callback(hObject, eventdata, handles) % hObject handle to stop (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) stop(handles.timer)
% --- Executes on button press in reset. function reset_Callback(hObject, eventdata, handles) % hObject handle to reset (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) stop(handles.timer) % initialize boids position and velocity boid.pos = 5000.*rand(handles.N,2);; boid.vel = [150.*rand(handles.N,1) zeros(handles.N,1)]; set(handles.timer,'UserData',boid)
function my_callback_fun(obj,event,handles)
% get data boid = get(obj,'UserData'); % move boids [boid.pos,boid.vel] = move_all_boids(boid.pos,boid.vel,handles); % plot boids boid_plotter(boid,handles) % update data set(obj,'UserData',boid);
function boid_plotter(boid,handles) h = axis(handles.axes1); plot(boid.pos(:,1),boid.pos(:,2),'o') axis([0 5000 0 5000]); set(gca,'XTick',[],'YTick',[]) hold on for i=1:size(boid.vel,1) plot([boid.pos(i,1) boid.pos(i,1)+boid.vel(i,1)],... [boid.pos(i,2) boid.pos(i,2)+boid.vel(i,2)],'r') end hold off
function [pos,vel] = move_all_boids(pos,vel,handles) h = findobj(gcf,'Style','slider'); behavior_strength = get(h,'Value'); % cycle through all boids for i=1:size(pos,1)
% calculate behaviorial velocity maneuvers
separation = 1*behavior_strength{1}*avoid(pos,i,200);
allignment = 5*behavior_strength{2}*match_vel(vel,i);
cohesion = .5*behavior_strength{3}*center_of_mass(pos,i);
% update boid
vel(i,:) = vel(i,:) + separation + allignment + cohesion;
% keep velocity magnitude constant (150)
vel(i,:) = 150*(vel(i,:)/norm(vel(i,:)));
% update position and restrict the values within the plot axis (mod.m, % modulus function)
pos(i,:) = mod(pos(i,:) + vel(i,:),5000);
end
%--------------------------------------------------------------- function v1 = center_of_mass(pos,i) % function to move each boid towards the precieved % center of mass of the rest of the flock % percieved center of mass pc = (sum(pos,1) - pos(i,:))/(size(pos,1)-1); % move the boid towards the center by 1% v1 = 0.01*(pc - pos(i,:)); %--------------------------------------------------------------- function v2 = avoid(pos,i,dist) % function to move each boid away from other boids v2 = [0 0]; for k = 1:size(pos,1) if k ~= i if norm(pos(i,:) - pos(k,:)) < dist v2 = (pos(i,:) - pos(k,:))-v2 ; end end end %--------------------------------------------------------------- function v3 = match_vel(vel,i) % function to match each boids velocity with near birds % note: same as rule one but with velocity rather than position pv = (sum(vel,1) - vel(i,:))/(size(vel,1) - 1); v3 = 0.01*(pv - vel(i,:));
% --- Executes on slider movement. function slider4_Callback(hObject, eventdata, handles) % hObject handle to slider4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties. function slider4_CreateFcn(hObject, eventdata, handles) % hObject handle to slider4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!