Error on GUI code

4 views (last 30 days)
Omiros Panteris
Omiros Panteris on 14 May 2019
Answered: Guillaume on 15 May 2019
function varargout = Temp_Soil(varargin)
% TEMP_SOIL MATLAB code for Temp_Soil.fig
% TEMP_SOIL, by itself, creates a new TEMP_SOIL or raises the existing
% singleton*.
%
% H = TEMP_SOIL returns the handle to a new TEMP_SOIL or the handle to
% the existing singleton*.
%
% TEMP_SOIL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TEMP_SOIL.M with the given input arguments.
%
% TEMP_SOIL('Property','Value',...) creates a new TEMP_SOIL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Temp_Soil_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Temp_Soil_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 Temp_Soil
% Last Modified by GUIDE v2.5 15-May-2019 00:26:10
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Temp_Soil_OpeningFcn, ...
'gui_OutputFcn', @Temp_Soil_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 Temp_Soil is made visible.
function Temp_Soil_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 Temp_Soil (see VARARGIN)
% Choose default command line output for Temp_Soil
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Temp_Soil wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Temp_Soil_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;
clear all
x = 0;
y = 0;
global a;
a=arduino('com6','mega2560','Libraries', 'I2C');
% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject handle to Start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x = 0;
y = 0;
global a;
a=arduino('com6','mega2560','Libraries', 'I2C');%Arduino connection
go = true;
while go
valueTemp = readVoltage(a,'A1');
temp = (valueTemp*100*0.48825125);
disp(temp);
x = [x temp];
plot(handles.axes1.x);
grid on;
xlabel('Time(seconds)')
ylabel('Temperature(°C)')
title('Real-Time Temperature Graph');
drawnow
set(handles.edit1,'String',num2str(temp));
pause(1);
valueSoil = redVoltage(a,'A0');
Soil=valueSoil;
disp(Soil);
y = [y Soil];
plot(handles.axes2.y);
grid on;
xlabel('Time(seconds)');
ylabel('Soil');
title('Real-Time Soil Graph');
drawnow
set(handles.edit2,'String',num2str(Soil));
pause(1);
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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
Error using Temp_Soil>Start_Callback (line 88)
MATLAB connection to Arduino Mega2560 at COM6 exists in your workspace. To
create a new connection, clear the existing object.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Temp_Soil (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Temp_Soil('Start_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
  3 Comments
Adam Danz
Adam Danz on 14 May 2019
Edited: Adam Danz on 15 May 2019
What part of the error message can I help you understand?
MATLAB connection to Arduino Mega2560 at COM6 exists in your workspace. To
create a new connection, clear the existing object.
Guillaume
Guillaume on 15 May 2019
Omiros' comment mistakenly posted as an answer moved here:
I believe that the error in gui_maifcn is the problem of having the other two errors.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 15 May 2019
I believe that the error in gui_maifcn is the problem of having the other two errors.
Well, to be precise the error comes from your Start_Callback function. Though, as Adam asked what is not clear about the error message: "MATLAB connection to Arduino Mega2560 at COM6 exists in your workspace", which basically tells you that you're already connected to the Arduino in your workspace. You cannot connect twice to the same device.
The error message even tells you how to fix it: " To create a new connection, clear the existing object."

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!