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

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

 채택된 답변

Geoff Hayes
Geoff Hayes 2016년 1월 13일
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

Rik - one problem is within the push button where you create and start the timer
handles.timer = timer('TimerFcn',{@timerfunction,handles},'ExecutionMode','FixedRate','Period',3);
In the above, handles is being passed into the timerfunction as a third parameter. Unfortunately, whenever this callback fires, handles will only be a copy of what it was when the callback was initialized. So it will never have the updated members/fields that you would expect when you do something like
guidata(hObject,handles);
from some other callback (or even from within itself). Look to my example whereby we pass in the handle to the figure
'TimerFcn',{@timerCallback,handles.figure1});
so that from within the callback we can use guidata to get the handles struct.
Geoff,
thnx man! I did some changes and it works now.
I still have one question. A few days ago I made a Createfcn and deleted it the wrong way, so i got an error: The variable chip_tijden_1 is not defined. I don't know how this is related to the CreatFcn, but after deleting it the proper way(Property inspector in guide) the error was gone.
Yesterday the Gui worked fine. But now, if i run the script, i get error again: The variable tijden_chip_1 is not defined. As you can see below, the variable is definetely defined. I don't have a createFcn in the gui, so that can't be the problem.
if isempty(getappdata(handles.gui1,'tijden_chip_1'))
tijden_chip_1 = {};
else
tijden_chip_1 = getappdata(handles.gui1,'tijden_chip_1');
end
Rik
Rik - which line is giving the error? Is it the condition in the if statement or something else? Please copy and paste the full error message.
Rik - but what is that line? It sounds like you are trying to use the variable before it has been instantiated. I would have to see line 144 and all code for the function that includes that line.
Rik's answer moved here
Geoff,
i have used ctr+f to search where i am using tijden_chip_1 for the first time and thats at line 280. this is the code before line 144:
conn = database('Data_Acces_Mylaps','rikwout','database');
Status = ping(conn);
if Status.AutoCommitTransactions == 'True'
Status = ('Connected to Access');
else
Status = ('Not connected to Access');
end
get(handles.text2,'String'); %tekst van text2 opvragen
set(handles.text2,'String',Status); %tekst van variabele "Status" weergeven
curs = exec(conn,'select Chip,MilliSecs, Location from Times');
curs = fetch(curs);
%%Lezen van gekozen data
mylaps_data = curs.Data;
handles.mylaps_data = mylaps_data;
%setorder zorgt ervoor dat het in dezelfde volgorde staat als in mylaps_data binnen komt
%met unique worden de unieke codes in een variabele gezet
setOrder = 'Stable';
chipcodes = unique(mylaps_data(:,1),setOrder);
%globale variable maken
handles.chipcodes = chipcodes;
%handles updaten
guidata(hObject,handles);
%namen die gekoppeld zijn aan chip laden(excel bestand)
filename = 'chips_namen.xlsx';
[~,txt,~] = xlsread(filename);
chips_namen = txt;
%%Chipcodes koppelen aan naam en edit text
%%Chip 1
%Wanneer er geen data is, de loop chip 1 hieronder niet uitvoeren no_data = strcmp('No Data',mylaps_data);
no_data = strcmp('No Data',mylaps_data);
if no_data == 0
as you can see, i don't use tijden_chip_1 before the error occurs. The error occurs when there is no data in the database. If there is some data in the database and i start the gui, the Gui runs the script without getting an error.
Rik
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 2016년 1월 20일
편집: Geoff Hayes 2016년 1월 20일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2016년 1월 12일

편집:

2016년 1월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by