"Attempt to reference field of non-structure array." when accessing a handle

2 views (last 30 days)
Ive written a GUI code which plots real time data from sensors With my GUI, i have a popup menu which lets the user select a value. In the function handle i store the chosen data into a handle "handles.numberofsensors".
The real time plotting is done by a timer function. I am able to access "handles.numberofsensors" from a push button callback function but I cant access "handles.numberofsensors" from the timerFcn of the timer object without the error.
Below are some excerpts from my code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function popupmenu_Callback(hObject, eventdata, handles)
contents = cellstr(get(hObject,'String'));
handles.numberofsensors = contents{get(hObject,'Value')};
% stores popupmenu value into the handle
fprintf('Number of sensors to be use: %s\n', handles.numberofsensors);
guidata(hObject, handles); %update handles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function start_button_Callback(hObject, eventdata, handles)
filename = 'C:\Program Files\xxxxxxxxxxxxxxxx';
d = get(handles.timer, 'UserData');
d.data = []; % array for the data to plot
d.data2 = [];
d.data3 = [];
% Figure/axis properties
axes(handles.axes1);
d.line_handle(1) = plot(NaN,NaN);
d.line_handle(2) = copyobj(d.line_handle(1),gca);
d.fid = fopen(filename, 'r');
set(handles.timer, 'UserData', d);
% Only START TIMER if it is not running
if strcmp(get(handles.timer, 'Running'), 'off')
start(handles.timer);
end
%ABLE TO ACCESS HANDLE HERE WITHOUT ERRORS
switch handles.numberofsensors
case '2'
fprintf('Producing real time plot for 2 sensors.\n');
case '4'
fprintf('Producing real time plot for 4 sensors.\n');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%timerfcn%%%%%
function updateFigure(hObject, eventdata, handles)
% Timer timer1 callback, called each time timer iterates.
% Reads in new data from text file, appends data to correct format
% and plots/updates the values in the plot.
d = get(hObject, 'UserData');
% read new data from file
tmp = readFile(hObject);
% append to array in user data
d.data = [d.data transpose(tmp{1})];
d.data2 = [d.data2 transpose(tmp{2})];
d.data3 = [d.data3 transpose(tmp{3})];
%Plots data according to how many sensors are chosen
#ERRORS HERE. im unable to access handles.numberofsensors anywhere in this function
switch handles.numberofsensors
case '2'
set(d.line_handle, 'XData', d.data2, 'YData', d.data);
case '4'
set(d.line_handle, 'XData', d.data2, 'YData', d.data);
set(d.line_handle(2), 'XData', d.data2, 'YData', d.data3, 'color','r');
end
set(hObject, 'UserData', d);
Pretty much if i try access 'handles.numnberofsensors' anywhere in the timerFcn above, ill get the "Attempt to reference field of non-structure array." error.

Answers (1)

Image Analyst
Image Analyst on 7 Feb 2014
Are you sure you began your timer after you attached numberofsensors to handles, and not before?

Categories

Find more on Interactive Control and Callbacks 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!