How to got correct answer in if condition in GUI?

1 次查看(过去 30 天)
Hi, after retrieve all data about driver got other problem, with if condition. When car number plate correct if condition work great, but if i do task that car number plate not in database i got error. Please help me, where i do mistake. Code add below. P.S. Sorry for my bad english
conn = database('baze', 'root', 'root', 'Vendor', 'MYSQL', 'Server', 'localhost', 'PortNumber', 3306);
setdbprefs('datareturnformat','structure');
word = get(handles.edit4,'String');
if iscell(word) && numel(word) == 1
word = word{1};
end
if ~ischar(word) || isempty(word);
error('A valid string must be supplied!');
end
sqlquery = ['select vardas, pavarde, laipsnis, pareigos, telefonas, marke, numeris, tarnyba, nuotrauka from info '...
'where numeris = ' '''' word ''''];
curs = exec(conn, sqlquery);
setdbprefs('DataReturnFormat','cellarray');
curs = fetch(curs);
numeris = curs.data{1,7};
vardas = curs.data{1,1};
pavarde = curs.data{1,2};
laipsnis = curs.data{1,3};
pareigos = curs.data{1,4};
telefonas = curs.data{1,5};
marke = curs.data{1,6};
tarnyba = curs.data{1,8};
nuotrauka = curs.data(1,9);
set(handles.edit5,'string',vardas);
set(handles.edit6,'string',pavarde);
set(handles.edit7,'string',laipsnis);
set(handles.edit8,'string',pareigos);
set(handles.edit9,'string',telefonas);
set(handles.edit10,'string',marke);
set(handles.edit11,'string',numeris);
set(handles.edit12,'string',tarnyba);
axes(handles.axes4);
nuotrauka = nuotrauka{1};
jimage = javax.imageio.ImageIO.read(java.io.ByteArrayInputStream(typecast(nuotrauka, 'uint8')));
height = jimage.getHeight;
width = jimage.getWidth;
pixels = reshape(typecast(jimage.getData.getDataStorage, 'uint8'), [3,width,height]);
img = cat(3, ...
transpose(reshape(pixels(3,:,:), [width,height])), ...
transpose(reshape(pixels(2,:,:), [width,height])), ...
transpose(reshape(pixels(1,:,:), [width,height])));
imshow(img);
a = ('ENTRY');
b = ('DO NOT ENTRY');
if word == curs.data{1,7}
set(handles.text13,'foregroundcolor','green');
set(handles.text13,'string',a);
else
set(handles.text13,'foregroundcolor','red');
set(handles.text13,'string',b);
end
close(curs);
close(conn);
clear all
  2 个评论
Jan
Jan 2016-12-18
If you get an error message, please post a complete copy of it. It is much easier to understand a problem, if it is known.
Gytis Raudonius
Gytis Raudonius 2016-12-20
Error using subsref Index exceeds matrix dimensions.
Error in cursor/subsref (line 42) [varargout{1:nargout}] = builtin('subsref',A,S);
Error in Pagrindas>pushbutton1_Callback (line 200) numeris = curs.data{1,7};
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in Pagrindas (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)Pagrindas('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2016-12-26
if isempty(curs.data)
%there was no match...
else
%okay to use the data
end
  11 个评论
Gytis Raudonius
Gytis Raudonius 2016-12-30
update code
curs = fetch(curs);
if isempty(curs) || ~isstruct(curs) || ~isfield(curs, 'data') || size(curs.data,2) < 7
set(handles.text13,'foregroundcolor','red');
set(handles.text13,'string','DO NOT ENTRY');
else
numeris = curs.data{7};
vardas = curs.data{1};
pavarde = curs.data{2};
laipsnis = curs.data{3};
pareigos = curs.data{4};
telefonas = curs.data{5};
marke = curs.data{6};
tarnyba = curs.data{8};
nuotrauka = curs.data(9);
set(handles.edit5,'string',vardas);
set(handles.edit6,'string',pavarde);
set(handles.edit7,'string',laipsnis);
set(handles.edit8,'string',pareigos);
set(handles.edit9,'string',telefonas);
set(handles.edit10,'string',marke);
set(handles.edit11,'string',numeris);
set(handles.edit12,'string',tarnyba);
axes(handles.axes4);
nuotrauka = nuotrauka{1};
jimage = javax.imageio.ImageIO.read(java.io.ByteArrayInputStream(typecast(nuotrauka, 'uint8')));
height = jimage.getHeight;
width = jimage.getWidth;
pixels = reshape(typecast(jimage.getData.getDataStorage, 'uint8'), [3,width,height]);
img = cat(3, ...
transpose(reshape(pixels(3,:,:), [width,height])), ...
transpose(reshape(pixels(2,:,:), [width,height])), ...
transpose(reshape(pixels(1,:,:), [width,height])));
imshow(img);
set(handles.text13,'foregroundcolor','green');
set(handles.text13,'string','ENTRY');
end
close(curs);
close(conn);
clear all
But have new problem, with this update if car plate not in database it write 'DO NOT ENRTY', but got same and if car plate in database

请先登录,再进行评论。

更多回答(2 个)

Jan
Jan 2016-12-18
A bold guess:
if word == curs.data{1,7}
Do not compare strings by ==, but with strcmp
if strcmp(word, curs.data{1,7})
  3 个评论
Jan
Jan 2016-12-26
Please do not let us guess, but post the error message you get. In the shown code you compare "word" with "curs.data{1,7}" twice.
Gytis Raudonius
Gytis Raudonius 2016-12-26
Error while evaluating uicontrol Callback
Error using subsref Index exceeds matrix dimensions.
Error in cursor/subsref (line 42) [varargout{1:nargout}] = builtin('subsref',A,S);
Error in Pagrindas>pushbutton1_Callback (line 200) numeris = curs.data{1,7};
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in Pagrindas (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)Pagrindas('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback

请先登录,再进行评论。


Gytis Raudonius
Gytis Raudonius 2016-12-30
solve full code
conn = database('baze', 'root', 'root', 'Vendor', 'MYSQL', 'Server', 'localhost', 'PortNumber', 3306);
setdbprefs('datareturnformat','structure');
word = get(handles.edit4,'String');
if iscell(word) && numel(word) == 1
word = word{1};
end
if ~ischar(word) || isempty(word);
error('A valid string must be supplied!');
end
sqlquery = ['select vardas, pavarde, laipsnis, pareigos, telefonas, marke, numeris, tarnyba, nuotrauka from info '...
'where numeris = ' '''' word ''''];
curs = exec(conn, sqlquery);
setdbprefs('DataReturnFormat','cellarray');
curs = fetch(curs);
set(handles.edit5,'string','');
set(handles.edit6,'string','');
set(handles.edit7,'string','');
set(handles.edit8,'string','');
set(handles.edit9,'string','');
set(handles.edit10,'string','');
set(handles.edit11,'string','');
set(handles.edit12,'string','');
axes(handles.axes4);
imshow(cla);
end
try
numeris = curs.data{7};
vardas = curs.data{1};
pavarde = curs.data{2};
laipsnis = curs.data{3};
pareigos = curs.data{4};
telefonas = curs.data{5};
marke = curs.data{6};
tarnyba = curs.data{8};
nuotrauka = curs.data(9);
set(handles.edit5,'string',vardas);
set(handles.edit6,'string',pavarde);
set(handles.edit7,'string',laipsnis);
set(handles.edit8,'string',pareigos);
set(handles.edit9,'string',telefonas);
set(handles.edit10,'string',marke);
set(handles.edit11,'string',numeris);
set(handles.edit12,'string',tarnyba);
axes(handles.axes4);
nuotrauka = nuotrauka{1};
jimage = javax.imageio.ImageIO.read(java.io.ByteArrayInputStream(typecast(nuotrauka, 'uint8')));
height = jimage.getHeight;
width = jimage.getWidth;
pixels = reshape(typecast(jimage.getData.getDataStorage, 'uint8'), [3,width,height]);
img = cat(3, ...
transpose(reshape(pixels(3,:,:), [width,height])), ...
transpose(reshape(pixels(2,:,:), [width,height])), ...
transpose(reshape(pixels(1,:,:), [width,height])));
imshow(img);
set(handles.text13,'foregroundcolor','green');
set(handles.text13,'string','LEIDŽIAMA');
catch
set(handles.text13,'foregroundcolor','red');
set(handles.text13,'string','DRAUŽIAMA');
end
close(curs);
close(conn);
clear all

类别

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