How to update values from a GUI's pushbutton in a while LOOP(actual code question)

12 Ansichten (letzte 30 Tage)
Hello, I created a GUI in matlab with 2 pushbuttons and a slider. I have managed to get the values returned by the pushbuttons to the workspace. Then I wrote a MATLAB program and in it I have a while loop. In that while loop I'm sending the values returned from the GUI pushbuttons and slider via a serial PORT. Problem is that once the program enters the while loop it stays in there forever meaning it will forever transmit the initial values of the pushbuttons and slider as I press them or move the slider again the program won't update those values in the while loop. If possible could someone please tell me what do I have to add in that while loop so I can get the values to update as I press the buttons and move the slider so I can send the new values via the serial port?
This is the program that I wrote:
global direction;
global sliderVal;
i = 1;
s = serial('COM3', 'BaudRate', 9600); %serial port object created
fopen(s) %opening serial port. Will take time as we are using Virtual Serial Port
while i == 1
fprintf(s, direction);
pwm = num2str(sliderVal);
fprintf(s, pwm) %send strings via the serial port
end
And this is how the GUI looks like and it's .m file:
function varargout = goodGUI(varargin)
% GOODGUI MATLAB code for goodGUI.fig
% GOODGUI, by itself, creates a new GOODGUI or raises the existing
% singleton*.
%
% H = GOODGUI returns the handle to a new GOODGUI or the handle to
% the existing singleton*.
%
% GOODGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GOODGUI.M with the given input arguments.
%
% GOODGUI('Property','Value',...) creates a new GOODGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before goodGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to goodGUI_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 goodGUI
% Last Modified by GUIDE v2.5 20-Sep-2016 16:57:44
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @goodGUI_OpeningFcn, ...
'gui_OutputFcn', @goodGUI_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 goodGUI is made visible.
function goodGUI_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 goodGUI (see VARARGIN)
% Choose default command line output for goodGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes goodGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = goodGUI_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 AddOneTag.
function AddOneTag_Callback(hObject, eventdata, handles)
% hObject handle to AddOneTag (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global direction
direction = 'cw';
function displayTag_Callback(hObject, eventdata, handles)
% hObject handle to displayTag (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,'String') returns contents of displayTag as text
% str2double(get(hObject,'String')) returns contents of displayTag as a double
% --- Executes during object creation, after setting all properties.
function displayTag_CreateFcn(hObject, eventdata, handles)
% hObject handle to displayTag (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in AddTwoTag.
function AddTwoTag_Callback(hObject, eventdata, handles)
global direction
direction = 'ccw';
% hObject handle to AddTwoTag (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 AddThreeTag.
function AddThreeTag_Callback(hObject, eventdata, handles)
global direction
direction = 'stop';
% hObject handle to AddThreeTag (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
global sliderVal;
sliderVal = get(hObject, 'Value');
% 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

Antworten (1)

Geoff Hayes
Geoff Hayes am 26 Sep. 2016
Ryan - I suspect the problem is because your while loop is is not interruptible and so any change that you make in the GUI (pressing a push button or moving the slider) does not get processed so long as the while loop condition is true. One way to get around this would be to put a pause in the loop body (for a fraction of a second) so that other events (like moving the slider) can be evaluated. This probably won't fix the problem since your global variables won't update within the loop. One way to get around this is to communicate directly with the GUI to get the direction and the sliderVal. In order to do this, you would need to set the set the GUI HandleVisibility property to on, and the Tag property to something unique like MyGui. Then your while loop body would look something like
k=1;
while k==1
hGui = findobj('Tag','MyGui');
if ~isempty(hGui)
handles = guidata(hGui);
sliderVal = get(handles.slider1, 'Value');
direction = '';
if isfield(handles,'direction') % check to ensure this field exists
direction = handles.direction;
end
fprintf(s, direction);
pwm = num2str(sliderVal);
fprintf(s, pwm) %send strings via the serial port
end
pause(0.01);
end
In the above, I use k instead of i since MATLAB uses this latter variable to represent the imaginary number. The only change to your GUI code would then be in the push button callbacks where you would need to update the handles structure with a direction field whenever a push button is pressed.
function AddOneTag_Callback(hObject, eventdata, handles)
handles.direction = 'cw';
guidata(hObject,handles);
function AddTwoTag_Callback(hObject, eventdata, handles)
handles.direction = 'ccw';
guidata(hObject,handles);
function AddThreeTag_Callback(hObject, eventdata, handles)
handles.direction = 'stop';
guidata(hObject,handles);
The above code is untested though should work and removes the need for the global variables. Though...do you really need a separate program (with the while loop)? Can't the GUI do the communication for you? Is it really necessary to send the same data across the port or can you just send the updates instead?
If you don't need to send the same direction and pwm value, then you could just do this whenever a change is made. So the push button and slider callbacks would then send the updated data. (This assumes that you have created and opened your serial port object in the OpeningFcn of your GUI, saving the object to the handles structure so that you can access it from your other callbacks.
And if you do need to send the data continually (even though there is no change), then consider using a timer that is created and started from within your GUI that periodically sends the direction and pwm data.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by