フィルターのクリア

Why is empty string test failing?

1 回表示 (過去 30 日間)
Ba Ba Black Sheep!
Ba Ba Black Sheep! 2017 年 4 月 20 日
コメント済み: dpb 2017 年 4 月 21 日
.
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 件のコメント
Walter Roberson
Walter Roberson 2017 年 4 月 21 日
Yes, what I posted does work.
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 Center および File ExchangeStructures についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by