error on line 42 : gui_mainfcn(gui_State, varargin{:});
14 次查看(过去 30 天)
显示 更早的评论
very new to coding. i have just run this code and i end up with this error. would like a bit of insight to know how i could fix it. thank you
the code:
function varargout = final_project(varargin)
% FINAL_PROJECT MATLAB code for final_project.fig
% FINAL_PROJECT, by itself, creates a new FINAL_PROJECT or raises the existing
% singleton*.
%
% H = FINAL_PROJECT returns the handle to a new FINAL_PROJECT or the handle to
% the existing singleton*.
%
% FINAL_PROJECT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FINAL_PROJECT.M with the given input arguments.
%
% FINAL_PROJECT('Property','Value',...) creates a new FINAL_PROJECT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before final_project_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to final_project_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to runbut (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help final_project
% Last Modified by GUIDE v2.5 29-May-2023 00:53:41
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @final_project_OpeningFcn, ...
'gui_OutputFcn', @final_project_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 final_project is made visible.
function final_project_OpeningFcn(hObject, ~, 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 final_project (see VARARGIN)
% Choose default command line output for final_project
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes final_project wait for user response (see UIRESUME)
% uiwait(handles.figure1);
delete(instrfind({'Port'},{'COM3'}))
clear a;
global a;
a = arduino();
% --- Outputs from this function are returned to the command line.
function varargout = final_project_OutputFcn(~, ~, 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 G_LED_ONbut.
function G_LED_ONbut_Callback(~, ~, ~)
% hObject handle to G_LED_ONbut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global a;
a.writeDigitalPin(a,D2,1)
% --- Executes on button press in G_LED_OFFbut.
function G_LED_OFFbut_Callback(~, ~, ~)
% hObject handle to G_LED_OFFbut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global a;
a.writeDigitalPin(a,D2,0)
% --- Executes on key press with focus on G_LED_ONbut and none of its controls.
function G_LED_ONbut_KeyPressFcn(~, ~, ~)
% hObject handle to G_LED_ONbut (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.UICONTROL)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Runbut.
function Runbut_Callback(~, ~, handles)
% hObject handle to Runbut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s = eval(get(handles.Samplestxt, 'string'));
axes(handles.Axes1);
title(Axes1,'Changes of IR Data')
xlabel('Changes of IR')
ylabel('Sample')
hold on
grid on
for x = 0: 0.01: s
global a;
y = readVoltage(a,'A0');
plot(x,y)
end
global a;
readIR = readVoltage(a,'A1');
set(handles.LItxt, 'string', readIR)
writePWMVoltage(a,'d3', readIR);
Lcount = 0;
Mcount = 0;
Hcount = 0;
if readIR < 1.25
Lcount = Lcount + 1;
elseif readIR > 1.25 && readIR < 3.75
Mcount = Mcount + 1;
elseif readIR > 3.75
Hcount = Hcount + 1;
end
q = [Lcount, Mcount, Hcount];
bar(q)
dist = {"low", "med", "high"};
set(gca, 'XTickLabel', dist)
% --- Executes on button press in Clearbut.
function Clearbut_Callback(~, ~, handles)
% hObject handle to Clearbut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.LItxt, 'string', '')
set(handles.Samplestxt, 'string', '')
cla
function Samplestxt_Callback(~, ~, ~)
% hObject handle to Samplestxt (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 Samplestxt as text
% str2double(get(hObject,'String')) returns contents of Samplestxt as a double
% --- Executes during object creation, after setting all properties.
function Samplestxt_CreateFcn(hObject, ~, ~)
% hObject handle to Samplestxt (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
function LItxt_Callback(~, ~, ~)
% hObject handle to LItxt (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 LItxt as text
% str2double(get(hObject,'String')) returns contents of LItxt as a double
% --- Executes during object creation, after setting all properties.
function LItxt_CreateFcn(hObject, ~, ~)
% hObject handle to LItxt (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
3 个评论
回答(1 个)
Walter Roberson
2023-5-28
Change
delete(instrfind({'Port'},{'COM3'}))
clear a;
global a;
a = arduino();
to
global a;
if ~isempty(a); clear global a; global a; end
a = arduino();
3 个评论
Walter Roberson
2023-5-28
As outside observers, we have little idea what the properties of handles.Samplestxt are . We know it has a String property, but we do not know whether it is a uicontrol style button or uicontrol style listbox or uicontrol style text or uicontrol style edit -- and if it uicontrol style edit, we have no reason to expect that its Max property is 1 (because if Max > 1 then String could potentially be a cell array of character vectors, depending on what the user entered in.
But why are you eval()'ing user input anyhow? You seem to be expecting that the field contains a numeric scalar; if that is the case use str2double() and make sure to test isfinite(s)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!