Verfasst am: 21.04.2012, 08:37
Titel: Gui Variable wird nicht aktualisiert
Morgen,
ich habe hier eine einfache Testfunktion in der mit der Auswahl des +-Zeichen das handles.ergebnis auf 10 gesetzt wird und beim Drücken des pushbuttons sollte da auch 10 stehen, aber man muss mehrmals zwischen + und - wechseln bis der richtige Wert über den pushbutton angezeigt wird.
Wie kriege ich diese Variable so global das die Änderung sofort übernommen wird und ich in anderen Funktionen drauf zugreifen kann?
Gui-Programmierung hab ich erst vor kurzem angefangen.
Code:
functionvarargout = gui_test2(varargin) % GUI_TEST2 M-file for gui_test2.fig % GUI_TEST2, by itself, creates a new GUI_TEST2 or raises the existing % singleton*.
%
% H = GUI_TEST2 returns the handle to a new GUI_TEST2 or the handle to % the existing singleton*.
%
% GUI_TEST2('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in GUI_TEST2.M with the given input arguments.
%
% GUI_TEST2('Property','Value',...) creates a new GUI_TEST2 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before gui_test2_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to gui_test2_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 gui_test2
% Last Modified by GUIDE v2.5 20-Apr-2012 18:34:03
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui_test2_OpeningFcn, ...
'gui_OutputFcn', @gui_test2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
ifnargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
% --- Executes just before gui_test2 is made visible. function gui_test2_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 gui_test2 (see VARARGIN)
% Choose default command line output for gui_test2
handles.output = hObject;
handles.ergebnis=hObject;
handles.ergebnis=0;
% UIWAIT makes gui_test2 wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. functionvarargout = gui_test2_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 when selected object is changed in uipanel1. function uipanel1_SelectionChangeFcn(hObject, eventdata, handles) % hObject handle to the selected object in uipanel1 % eventdata structure with the following fields (see UIBUTTONGROUP) % EventName: string 'SelectionChanged' (read only) % OldValue: handle of the previously selected object or empty if none was selected % NewValue: handle of the currently selected object % handles structure with handles and user data (see GUIDATA)
switchget(get(handles.uipanel1,'SelectedObject'),'String') case '+'
fprintf('+\n');
handles.ergebnis=10;
guidata(hObject,handles);
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.text1,'String',num2str(handles.ergebnis));
Was ist die Default-Auswahl im Popup? Wenn es + ist, dann solltest du in deiner OpeningFcn auch 10 als Default wählen. Ansonsten musst du in der Tat auf - und dann wieder auf + gehen, damit das übernommen wird.
Falls weitere Probleme bestehen, bitte die .fig zur Verfügung stellen, da man den Code ohne nicht laufen lassen kann.
Hi, ich hab nun folgendes Problem wenn ich eine Event-Funktion für die RS232 schnittstelle hinzufüge, dieses Event tritt ein wenn ein 'CR/LF' folgt, ist als Terminator definiert.
Wenn pushbutton gedrückt wird, werden Kommandos über die serielle Schnittstelle rausgehauen, danach bekomme ich eine Antwort. Er springt bei Empfang des Terminators in die "myCallback" Funktion, darin kann ich über das handle nicht den "text" m.H. von set(handles.getVal,'String',num2str(test)); ändern.
Woran könnte es liegen? Habe ja shcon der "myCallback" Funktion handles übergeben
Der Code dazu:
Code:
functionvarargout = test4(varargin) % TEST4 M-file for test4.fig % TEST4, by itself, creates a new TEST4 or raises the existing % singleton*.
%
% H = TEST4 returns the handle to a new TEST4 or the handle to % the existing singleton*.
%
% TEST4('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in TEST4.M with the given input arguments.
%
% TEST4('Property','Value',...) creates a new TEST4 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before test4_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to test4_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 test4
% Last Modified by GUIDE v2.5 23-Apr-2012 11:16:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test4_OpeningFcn, ...
'gui_OutputFcn', @test4_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
ifnargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
% --- Executes just before test4 is made visible. function test4_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 test4 (see VARARGIN)
% Choose default command line output for test4
handles.output = hObject;
% UIWAIT makes test4 wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. functionvarargout = test4_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 button press in readVal. function readVal_Callback(hObject, eventdata, handles) % hObject handle to readVal (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of readVal
%**************************************************************************
function myCallback(hObject, eventdata, handles) %Event wenn 'CR/LF' empfangen
%**************************************************************************
??? Error using ==> guidata at 89
H must be the handle to a figure or figure descendent.
Der Aufruf von GUIDATA enthält also keinen Figure-Handle. Dann wäre es hilfreich, sich diesen Aufruf anzuschauen und ihn zu verbessern. Das Ändern des Callbacks ist dabei zunächstmal nicht hilfreich.
Die Callback sehen oft so aus:
Code:
handles.pHandle.BytesAvailableFcn = {@myCallback,handles};
... function myCallback(objH, EventData, handles)
handles = guidata(handles.Figure); % handles.Figure ist Handle der Figure
Bitte nicht erschrecken, es ist sehr viel Code.
Das Problem ist die "MyCallback-Funktion". Diese wird immer dann aufgerufen wenn ein Byte über die Serielle geschickt wird, das ein "CR/LF" enthält.
Wenn ich den Button "Slave Adresse 1 auslesen" betätige werden paar Kommandos über die serielle schnittstelle geschickt, das funzt soweit.
Hab deinen Vorschlag reingepackt.
Sobald aber etwas über die Serielle Schnittstelle zurückkommt , wird die MyCallback aufgerufen und es kommt die folgende Fehlermeldung:
Das Problem liegt irgendwie in der "MyCallback-Funktion" die z.B. beim Auslesen der Slave Adresse die handles nicht kennt.
Code:
functionvarargout = thermopile_gui(varargin) % THERMOPILE_GUI M-file for thermopile_gui.fig % THERMOPILE_GUI, by itself, creates a new THERMOPILE_GUI or raises the existing % singleton*.
%
% H = THERMOPILE_GUI returns the handle to a new THERMOPILE_GUI or the handle to % the existing singleton*.
%
% THERMOPILE_GUI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in THERMOPILE_GUI.M with the given input arguments.
%
% THERMOPILE_GUI('Property','Value',...) creates a new THERMOPILE_GUI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before thermopile_gui_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to thermopile_gui_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 thermopile_gui
% Last Modified by GUIDE v2.5 23-Apr-2012 17:24:44
% Begin initialization code - DO NOT EDIT
%global pHandle;
% --- Executes just before thermopile_gui is made visible. function thermopile_gui_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 thermopile_gui (see VARARGIN)
% Choose default command line output for thermopile_gui
handles.output = hObject;
% UIWAIT makes thermopile_gui wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. functionvarargout = thermopile_gui_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 selection change in sel_com_port. function sel_com_port_Callback(hObject, eventdata, handles) % hObject handle to sel_com_port (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns sel_com_port contents as cell array % contents{get(hObject,'Value')} returns selected item from sel_com_port
% --- Executes during object creation, after setting all properties. function sel_com_port_CreateFcn(hObject, eventdata, handles) % hObject handle to sel_com_port (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. ifispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in sel_baudrate. function sel_baudrate_Callback(hObject, eventdata, handles) % hObject handle to sel_baudrate (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns sel_baudrate contents as cell array % contents{get(hObject,'Value')} returns selected item from sel_baudrate
% --- Executes during object creation, after setting all properties. function sel_baudrate_CreateFcn(hObject, eventdata, handles) % hObject handle to sel_baudrate (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. ifispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in connect_com_port. function connect_com_port_Callback(hObject, eventdata, handles) % hObject handle to connect_com_port (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
popup_sel_com_port=get(handles.sel_com_port,'Value'); %Welcher Com-Port wurde ausgewaehlt
popup_sel_baud=get(handles.sel_baudrate,'Value'); %Welche Baudrate wurde ausgewaehlt
%--- Open COM
fopen(handles.pHandle);
handles.pHandle.status;
if handles.pHandle.status ~= 'open'
warning('COM-Port isn´t open');
end
if handles.pHandle.status == 'open'
fprintf('COM-Port offen\n');
handles.flagSerialOpen=1;
guidata(hObject,handles);
%set(handles.com_status,'Visible','on');
set(handles.disconnect_com_port,'Enable','on');
set(handles.connect_com_port,'Enable','off');
end
% --- Executes on button press in read_slaveAdr1. function read_slaveAdr1_Callback(hObject, eventdata, handles) % hObject handle to read_slaveAdr1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties. function get_slaveAdr1_CreateFcn(hObject, eventdata, handles) % hObject handle to get_slaveAdr1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called guidata(hObject, handles); % Update handles structure
%function myCallback(event,hObject,handles)%(hfig)%hObject%handles
function myCallback(objH,EventData)
%
%**************************************************************************
%global pHandle;
%global slaveAdr;
handles=guidata(objH);
global choice;
% --- Executes during object deletion, before destroying properties. function get_slaveAdr1_DeleteFcn(hObject, eventdata, handles) % hObject handle to get_slaveAdr1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties. function get_Emission1_CreateFcn(hObject, eventdata, handles) % hObject handle to get_Emission1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in read_emission1. function read_emission1_Callback(hObject, eventdata, handles) % hObject handle to read_emission1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% --- Executes when selected object is changed in panel_sel_Sensor. function panel_sel_Sensor_SelectionChangeFcn(hObject, eventdata, handles) % hObject handle to the selected object in panel_sel_Sensor % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) switchget(get(handles.panel_sel_Sensor,'SelectedObject'),'String') case 'Melexis'
handles.sensor='1';
guidata(hObject,handles);
case 'Excelitas'
handles.sensor='2';
guidata(hObject,handles);
case 'Heimann'
handles.sensor='3';
guidata(hObject,handles);
end
% --- Executes when selected object is changed in panel_sel_Anz. function panel_sel_Anz_SelectionChangeFcn(hObject, eventdata, handles) % hObject handle to the selected object in panel_sel_Anz % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
switchget(get(handles.panel_sel_Anz,'SelectedObject'),'String') case '1'
% --- Executes when selected object is changed in panel_sel_Typ. function panel_sel_Typ_SelectionChangeFcn(hObject, eventdata, handles) % hObject handle to the selected object in panel_sel_Typ % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
switchget(get(handles.panel_sel_Typ,'SelectedObject'),'String') case '1x1'
handles.typ='1';
case '8x1'
handles.typ='2';
case '16x4'
handles.typ='3';
case '16x16'
handles.typ='4';
end
% --- Executes on button press in read_slaveAdr2. function read_slaveAdr2_Callback(hObject, eventdata, handles) % hObject handle to read_slaveAdr2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in read_emission2. function read_emission2_Callback(hObject, eventdata, handles) % hObject handle to read_emission2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%set(handles.get_Emission1,'String',num2str(emissionVal));
end
% --- Executes on button press in disconnect_com_port. function disconnect_com_port_Callback(hObject, eventdata, handles) % hObject handle to disconnect_com_port (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
handles.flagSerialOpen if handles.flagSerialOpen==1
set(handles.disconnect_com_port,'Enable','off');
set(handles.connect_com_port,'Enable','on');
end
% --- Executes when selected object is changed in panel_sel_darstellung. function panel_sel_darstellung_SelectionChangeFcn(hObject, eventdata, handles) % hObject handle to the selected object in panel_sel_darstellung % eventdata structure with the following fields (see UIBUTTONGROUP) % EventName: string 'SelectionChanged' (read only) % OldValue: handle of the previously selected object or empty if none was selected % NewValue: handle of the currently selected object % handles structure with handles and user data (see GUIDATA)
switchget(get(handles.panel_sel_darstellung,'SelectedObject'),'String') case 'Real Time Plot'
fprintf('RealTime\n');
%set(handles.arrayFigHandle,'Visible','off');
% --- Executes on button press in startSampling. function startSampling_Callback(hObject, eventdata, handles) % hObject handle to startSampling (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
end if handles.RecFlag==1 fprintf('aussen\n');
for z=1:1%nmbrArrayRow
fprintf('Innen\n');
x=3;
for j=1:16%nmbrArrayCol
px_n=handles.captBuf(x,1)+(handles.captBuf(x+1,1)*256)
x=x+2;
%LByte=fread(pHandle,1,'char');
%HByte=fread(pHandle,1,'char');
%temp(i,j)=(LByte+HByte*256)*0.02-273.15
%data(i,j)=(temp(i,j)+40)*256/125;
end end
handles.SendFlag=1; %next sample
handles.RecFlag=0; %data processed
guidata(hObject,handles);
end end
% --- Executes on button press in stopSampling. function stopSampling_Callback(hObject, eventdata, handles) % hObject handle to stopSampling (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global StartSamplFlag;
Nabend,
mir lässt mein Programm einfach keine Ruhe...
Habe noch dazu ein ähnliches Minimalbeispiel im Internet gefunden, habs etwas angepasst und da funktioniert es. Hilft das vielleicht bei der Lösung meines Problems weiter?
Kurze Beschreibung:
Button wird gedrückt >> Serielle Shcnittstelle wird eingerichtet.
Wird ein 'S' über das Terminal geschickt wird die Mycallback aufgerufen und der Text in 200 geändert.
Code:
functionvarargout = test5(varargin) % TEST5 M-file for test5.fig % TEST5, by itself, creates a new TEST5 or raises the existing % singleton*.
%
% H = TEST5 returns the handle to a new TEST5 or the handle to % the existing singleton*.
%
% TEST5('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in TEST5.M with the given input arguments.
%
% TEST5('Property','Value',...) creates a new TEST5 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before test5_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to test5_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 test5
% Last Modified by GUIDE v2.5 25-Apr-2012 20:09:59
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test5_OpeningFcn, ...
'gui_OutputFcn', @test5_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
ifnargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
% --- Executes just before test5 is made visible. function test5_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 test5 (see VARARGIN)
% Choose default command line output for test5
handles.output = hObject;
% UIWAIT makes test5 wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. functionvarargout = test5_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 button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
strPortDes = 'COM11';
handles.pHandle = serial(strPortDes);
handles.pHandle.BaudRate = 9600;
Das Problem ist nur das die Variable handles.RecFlag in der myCallback mit guidata(FigureObject,handles); nicht aktualisiert wird.
In der Funktion "startSampling_Callback" bleibt diese Variable immer 0, hat jmd ne Idee was ich da falsch mache?
Grüße
Einstellungen und Berechtigungen
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.