Why is empty string test failing?

1 次查看(过去 30 天)
.
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

采纳的回答

dpb
dpb 2017-4-20
>> 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 个评论
dpb
dpb 2017-4-21
OK, so the comparison must have new smarts internally to match up multiple classes on either side of the logical operators...

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by