Updating a variable(not copying) in gui made in guide

1 view (last 30 days)
hi guys,
I made a Gui that is connected to an Access database. the function 'refreshData' is getting updated by a timer every x seconds. Im using handles in the refreshData so the uitable and the listbox can use those variables. Im aware that after refreshData ends the variables are not updated with just using handles en guidata. I have read a lot on the internet but I don't really get it.
The length of variable 'Tijden_chip_1' changes everytime refreshData gets updated. At the end of refreshData I want to update the variable Tijden_chip_1 so the Gui can use the new(bigger) length in the script when refreshData runs again.
(1) the variable Tijden_chip_1 starts empty en gets bigger everytime refreshData runs again. So I have to define Tijden_chip_1 somewhere as an empty cell, otherwise i get the error: variable 'Tijden_chip_1' is not defined. I defined Tijden_chip_1 in the Gui_openingsFcn, but I am not sure that's a good idea.
(2) I have used setappdata(handles.output,'Tijden_chip_1_update',Tijden_chip_1) and guidata(handles.output) to update the variable to the output handles, so I can use getappdata(handles.output,'Tijden_chip_1_update') in the function refreshData. This didn't work.
(3) I used break to detect where it went wrong. If I put the break right before I am using setappdata, Tijden_chip_1 is a 2-by-3 cell array. If I put the break behind setappdata, the workspace says its a 2-by-3 cell array, but if I open the variable its actually a 6-by-3 cell array.
my questions: - Where can if define a variable Tijden_chip_1, which is an empty cell array, that has the same data as Tijden_chip_1 at the end of the script in resfreshData? - Is my approach, updating the data in Tijden_chip_1 to the handles.output, a good idea? Or should i update it to somewhere else? - How is it possible the workspace says: its an 3-by-2 cell array, but if open Tijden_chip_1 it's actually a 6-by-2 array?
I hope someone can help me. Sorry for my bad english.
kind regards,
Rik Wout

Accepted Answer

Geoff Hayes
Geoff Hayes on 13 Jan 2016
Rik - without having access to all of your code, it is difficult to answer all of your questions. Typically, for variables that are accessed by the different callbacks (within the GUI), I would use guidata to update the handles structure with any app data that may be needed to be shared. For example, I would add a field named Tijden_chip_1 to the handles structure in the OpeningFcn of your GUI as
function myGui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.Tijden_chip_1 = {};
guidata(hObject, handles);
The order in the above is important. We initialize the field Tijden_chip_1 to be an empty cell array of the handles structure, and then we call guidata to save that updated handles structure. Other callbacks will then have access to this field and the data. To update it, in another callback you could do something like
chipData = handles.Tijden_chip_1;
chipData = [chipData ; newChipData];
handles.Tijden_chip_1 = chipData;
guidata(hObject,handles); % saves the updated Tijden_chip_1 field
Please note that the handles structure is not an input to the timer callback, so you will have to do something a little different here. When you create the timer, pass in the GUI figure handle as an input. Something like
handles.timer = timer('Name','MyTimer', ...
'Period',15, ...
'StartDelay',1, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@timerCallback,handles.figure1});
guidata(hObject,handles);
start(handles.timer);
Then, in the timer callback, you would make use of this input as
function [] = timerCallback(~,~,guiHandle)
if ~isempty(guiHandle)
% get the handles
handles = guihandles(guiHandle);
if isfield(handles,'Tijden_chip_1')
% maybe update this field
chipData = handles.Tijden_chip_1;
chipData = [chipData ; newChipData];
handles.Tijden_chip_1 = chipData;
guidata(hObject,handles); % saves the updated Tijden_chip_1 field
end
end
Try the above and see what happens!
  7 Comments
Geoff Hayes
Geoff Hayes on 19 Jan 2016
Rik - but what is line 144? You've shown the code up until that point, but what does the subsequent line do such that it generates the error?
Geoff Hayes
Geoff Hayes on 20 Jan 2016
Edited: Geoff Hayes on 20 Jan 2016
Rik - the error is being raised from the TimerFcn. Can you just post that code? i.e. all code from that function. I'm just wondering since that is running in the background (every 15 seconds?) then perhaps this has nothing to do with the breakpoint that you have put in the code and the stepping to the subsequent lines.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!