Filter löschen
Filter löschen

Why is empty string test failing?

1 Ansicht (letzte 30 Tage)
Ba Ba Black Sheep!
Ba Ba Black Sheep! am 20 Apr. 2017
Kommentiert: dpb am 21 Apr. 2017
.
This GUI's source code is giving me error message:
function browsePushButton_Callback(hObject, eventdata, handles)
% hObject handle to browsePushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% show open file dialog
[filename, pathname] = uigetfile({ '*.jpg'; '*.png';'*.bmp';'*.jpeg'; }, ...
'Open image', ...
'' ...
);
% obtain image-file's path
imagePath = strcat(pathname, filename);
% this test is failing
if (imagePath ~= '')
image = imread(imagePath);
% digging out image related info
[pathstr,name,ext] = fileparts(filename) ;
fileinfo = imfinfo(imagePath);
FileSize1 = fileinfo.FileSize(1,1);
width = fileinfo.Width;
height = fileinfo.Height;
%
axes(handles.imagesPictureBox);
imshow(image);
else
h = msgbox('Invalid Value', 'Error','error');
end
Error message
Error using ~=
Matrix dimensions must agree.
Error in OpenFileDialogBoxTest>browsePushButton_Callback (line 91)
if (imagePath ~= '')
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in OpenFileDialogBoxTest (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)OpenFileDialogBoxTest('browsePushButton_Callback',
hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback

Akzeptierte Antwort

dpb
dpb am 20 Apr. 2017
>> p=cd
p =
C:\ML_R2014b\work
>> whos p
Name Size Bytes Class Attributes
p 1x17 34 char
>> if p~=''
,disp('not empty'),end
Error using ~=
Matrix dimensions must agree.
>>
imagePath is an array of characters just as the variable p above; Matlab relational operators are vectorized and do a comparison element-by-element. Above you're trying to compare an array of length(imagePath) to an empty (zero-length) array so if there is anything in the variable the LH and RH lengths are incompatible; hence the error.
Use isempty instead, that's what it's for...
Example using above illustration...
>> p='';
>> isempty(p)
ans =
1
>> p=cd;
>> isempty(p)
ans =
0
>>
  4 Kommentare
Walter Roberson
Walter Roberson am 21 Apr. 2017
Yes, what I posted does work.
dpb
dpb am 21 Apr. 2017
OK, so the comparison must have new smarts internally to match up multiple classes on either side of the logical operators...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by