If I just run the function bit using F9, I get a 'Function definition not permitted in this context' error.
Using a script to change uicontrol values in a gui
4 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
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
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!