How to got correct answer in if condition in GUI?

조회 수: 1 (최근 30일)
Gytis Raudonius
Gytis Raudonius 2016년 12월 18일
답변: Gytis Raudonius 2016년 12월 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일
Still got same error :(
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