Why is empty string test failing?

2 views (last 30 days)
Ba Ba Black Sheep!
Ba Ba Black Sheep! on 20 Apr 2017
Commented: dpb on 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

Accepted Answer

dpb
dpb on 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 Comments
Walter Roberson
Walter Roberson on 21 Apr 2017
Yes, what I posted does work.
dpb
dpb on 21 Apr 2017
OK, so the comparison must have new smarts internally to match up multiple classes on either side of the logical operators...

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!