How to send a value from matlab to mysql?

3 次查看(过去 30 天)
Hello,
I need to send the value handles.Data to my sql table, but I keep getting a error and don't know how to solve this problem. The value's that goes in to Data are coming from the arduino with a potmeter. And everything works except de sending part. So the function verstuur_calback and then the query=.... code doesn't work.
Could someone help me solve this? Maybe I need to put it in a for loop or something like that, but I don't know.
function inputdata_Callback(hObject, eventdata, handles)
% hObject handle to inputdata (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
board= arduino();
b=0;
for i= 1:1:10
analog= readVoltage (board, 'A0');
writePWMVoltage (board, 'D3', analog);
disp(['analog= ', num2str(analog)]);
pause(1);
A(i)= analog;
b= b+1;
B(i)= b;
end
A=A';
B=B';
handles.Data= [B A];
disp(handles.Data);
colnames= {'PK','Voltage'};
set(handles.uitable2,'data',handles.Data ,'ColumnName',colnames);
guidata(hObject, handles);
function verstuur_Callback(hObject, eventdata, handles)
% hObject handle to verstuur (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
gebruikersnaam= get(handles.edit_gebruikersnaam, 'String');
wachtwoord= get(handles.edit_wachtwoord, 'String');
datasource= get(handles.edit_tabel, 'String');
conn= database(datasource, gebruikersnaam, wachtwoord, handles.driver, handles.url);
query= ['INSERT INTO ' table ' VALUES (''''Data'''')'];
execute(conn,query);
close (conn);
  2 个评论
Geoff Hayes
Geoff Hayes 2019-4-12
编辑:Geoff Hayes 2019-4-12
Martijn - what is the full error message? Is the error from
query= ['INSERT INTO ' table ' VALUES (''''Data'''')'];
(not sure where Data is defined...)
Maybe Guillaume's answer at https://www.mathworks.com/matlabcentral/answers/455843-putting-two-seperate-columns-in-to-one-variable-and-how-to-insert-a-array-into-mysql will help. If this question is in response to his answer, then please further the conversation there rather than creating a new question.
Martijn Roks
Martijn Roks 2019-4-13
This is my full error, I think Data needs to be handles.Data. Because that is where I saved the values from the potmeter (voltage) in de function before.
% Error using Week7>verstuur_Callback (line 158)
%All input arguments must be tables.
%Error in gui_mainfcn (line 95)
% feval(varargin{:})
%Error in Week7 (line 42)
% gui_mainfcn(gui_State, varargin{:});
%Error in
%matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Week7('verstuur_Callback',hObject,eventdata,guidata(hObject))
%Error while evaluating UIControl Callback.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by