Using a script to change uicontrol values in a gui

조회 수: 8 (최근 30일)
avram alter
avram alter 2019년 9월 16일
편집: avram alter 2019년 9월 17일
I have a code that initializes a GUI, and then take serial data, compares it to excel master files, and then changes the colors of some uipanels in the gui. this is the code, the comments are generally prototyping or holdovers from other codes I incorporated:
delete(instrfind('Port', 'COM3'));
tag = serial('COM3'); %check which port is used
fopen(tag);
MyGUI;
BOX = char(zeros(2,14));
i=1;
c=0;
TrueValueData = 'C:\MasterCodes.xlsx';
[~,~,TrueValMat] = xlsread(TrueValueData); % Creates matrix filled with the correct values,
% indexed by box, which is the first row
% all proceeding rows are the master value
function result(handles)
for i=1:9223372036854775807 %just keeps looping, will probably replace with a push button to kill
if i>10 %first couple reads are filled with unicode nonsense, this skips that stage
readData = fscanf(tag);
if length(readData)>12
BOX(str2num(readData(8)),1:14)= readData(11:24); % these numbers just give us what we want;
% tags come in initially with some gobbledy-gook
end
%
% if(length(readData)>10) %if chip has been read
%
% ReadChips
if strcmp(TrueValMat{2,1}, BOX(1,:))
set(handles.uipanel1, 'BackgroundColor', 'green');
else
set(handles.uipanel1, 'BackgroundColor', 'red');
end
if strcmp(TrueValMat{2,2}, BOX(2,:))
set(handles.uipanel2, 'Backgroundcolor', 'green');
else
set(handles.uipanel2, 'Backgroundcolor', 'red');
end
if strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:)) == 1
break
end
end
end
end
function uipanel1_Callback(hObject, eventdata, handles)
result;
function uipanel2_Callback(hObject, eventdata, handles)
result;
end
end
The problem is that this doesnt actually do anything, the GUI comes up, but the loop doesn't iterate, and the colors don't change in the GUI.
edit: added in loop break point
  댓글 수: 1
avram alter
avram alter 2019년 9월 16일
If I just run the function bit using F9, I get a 'Function definition not permitted in this context' error.

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

채택된 답변

Image Analyst
Image Analyst 2019년 9월 16일
Just build all that code into the m-file that GUIDE made for you. Why control your GUI from a separate, external script? If you have some settings that you need to specify, just use a pulldown menu to bring up a separate sub-GUI then return the settings to the main GUI.
  댓글 수: 3
Image Analyst
Image Analyst 2019년 9월 17일
Make a new, second GUI in GUIDE called AdjustSettings.m with all the controls you want on it. Then, in the exit function of that GUI, read all the settings from the controls and put them into fields of a structure, then pass that structure out, for example as handles.output:
guiSettings.scrollbar1 = handles.scrollbar1.Value;
guiSettings.popup1 = handles.popup1.Value;
guiSettings.edit1 = handles.edit1.String;
handles.output = guiSettings;
Do that for whatever controls you want to pass the state of back to your main GUI.
Now, call that GUI like this from your main GUI:
guiSettings = AdjustSettings(guisettings);
You can make guiSettings be global for example by the global keyword, or using getappdata() and setappdata(). In the OpeningFcn() of your AdjustSettings GUI, you can get guiSettings from varargin
guiSettings = varargin{1};
if ~isempty(guiSettings)
% Transfer incoming/input/existing values to the controls on this GUI.
handles.scrollbar1.Value = guiSettings.scrollbar1;
handles.popup1.Value = guiSettings.popup1;
handles.edit1.String = guiSettings.edit1;
end
avram alter
avram alter 2019년 9월 17일
편집: avram alter 2019년 9월 17일
I don't think I have any settings that I would need a sub-GUI for. I jsut need to know where in MyGUI I put my script in order for it to work. All I need to do is a string comparison, and then that changes the color of the panel. But I dont know where in MyGUI.m I would put my string comparison script, and how to call panels to just channge color.
I really appreciate the time you put into this, but I am not sure that answer applies to my situation.
I put the coe into the actual GUI.m file as suggested, this is what I have now:
function varargout = WorkingGUI(varargin)
% WORKINGGUI MATLAB code for WorkingGUI.fig
% WORKINGGUI, by itself, creates a new WORKINGGUI or raises the existing
% singleton*.
%
% H = WORKINGGUI returns the handle to a new WORKINGGUI or the handle to
% the existing singleton*.
%
% WORKINGGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WORKINGGUI.M with the given input arguments.
%
% WORKINGGUI('Property','Value',...) creates a new WORKINGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before WorkingGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to WorkingGUI_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 WorkingGUI
% Last Modified by GUIDE v2.5 17-Sep-2019 13:20:06
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @WorkingGUI_OpeningFcn, ...
'gui_OutputFcn', @WorkingGUI_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 WorkingGUI is made visible.
function WorkingGUI_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 untitled3 (see VARARGIN)
% Choose default command line output for WorkingGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes WorkingGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = WorkingGUI_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;
%beginning of script
delete(instrfind('Port', 'COM3'));
tag = serial('COM3'); %check which port is used
fopen(tag);
BOX = char(zeros(2,14));
i=1;
c=0;
TrueValueData = 'C:\MasterCodes.xlsx';
[~,~,TrueValMat] = xlsread(TrueValueData); % Creates matrix filled with the correct values,
% indexed by box, which is the first row
% all proceeding rows are the master value
for i=1:9223372036854775807
if i>10 %first couple reads are filled with unicode nonsense, this skips that stage
readData = fscanf(tag);
if length(readData)>12
BOX(str2num(readData(8)),1:14)= readData(11:24); % these numbers just give us what we want;
% tags come in initially with some gobbledy-gook
end
%
% if(length(readData)>10) %if chip has been read
%
% ReadChips
if strcmp(TrueValMat{2,1}, BOX(1,:))
Reader1Correct;
else
Reader1Incorrect;
end
if strcmp(TrueValMat{2,2}, BOX(2,:))
Reader2Correct;
else
Reader2Incorrect;
end
if strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:)) == 1
break
end
end
end
% --- Executes during object creation, after setting all properties.
function uipanel1_CreateFcn(hObject, eventdata, handles)
function Reader1Correct
set(handles.uipanel1, 'BackgroundColor', 'g');
end
function Reader1Incorrect
set(handles.uipanel1, 'BackgroundColor', 'r');
end
% hObject handle to uipanel1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function uipanel2_CreateFcn(hObject, eventdata, handles)
function Reader2Correct
set(handles.uipanel2, 'BackgroundColor', 'g');
end
function Reader2Incorrect
set(handles.uipanel2, 'BackgroundColor', 'r');
end
end
end
end
end
% hObject handle to uipanel2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
Unfortunately, I am still not getting anything to even pop up now. I dont know if I called the functions in the correct way, But I am getting this error:
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)WorkingGUI('uipanel1_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'WorkingGUI_OutputFcn'.
Error in gui_mainfcn (line 264)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in WorkingGUI (line 42)
gui_mainfcn(gui_State, varargin{:});
and I am not usre how to deal with that. thanks again
edit: fixed my end related errors
my code has changed enough that I made a new thread, it can be found here https://www.mathworks.com/matlabcentral/answers/480824-feval-error-in-gui-implementation

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by