Effacer les filtres
Effacer les filtres

Why is empty string test failing?

1 vue (au cours des 30 derniers jours)
Ba Ba Black Sheep!
Ba Ba Black Sheep! le 20 Avr 2017
Commenté : dpb le 21 Avr 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

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by