it says error using assert, assertion failed and error in iuntitled (line 10) assert(ischar(in))

조회 수: 34 (최근 30일)
error using assert
Assertion failed
Error in iuntitled (line 10)
assert(ischar(in))
  댓글 수: 3
Ian
Ian 2022년 6월 16일
편집: Geoff Hayes 님. 2022년 6월 17일
heres the code
function result = encodeCharacter(in)
% Encode a single character into the 16-bit sequence for the Code 39
% barcode format.
%
% Rupert Thomas, March 2016
%
% Thanks to (for the codes): % http://notionovus.com/blog/code-39-barcode-biscuits/#setup
assert(ischar(in));
assert(length(in)==1);
codesArray = {
'1010001110111010', '1110100010101110', '1011100010101110', '1110111000101010', ... % 0, 1, 2, 3
'1010001110101110', '1110100011101010', '1011100011101010', '1010001011101110', ... % 4, 5, 6, 7
'1110100010111010', '1011100010111010', '1110101000101110', '1011101000101110', ... % 8, 9, A, B
'1110111010001010', '1010111000101110', '1110101110001010', '1011101110001010', ... % C, D, E, F
'1010100011101110', '1110101000111010', '1011101000111010', '1010111000111010', ... % G, H, I, J
'1110101010001110', '1011101010001110', '1110111010100010', '1010111010001110', ... % K, L, M, N
'1110101110100010', '1011101110100010', '1010101110001110', '1110101011100010', ... % O, P, Q, R
'1011101011100010', '1010111011100010', '1110001010101110', '1000111010101110', ... % S, T, U, V
'1110001110101010', '1000101110101110', '1110001011101010', '1000111011101010', ... % W, X, Y, Z
'1000101011101110', '1110001010111010', '1000111010111010', '1000100010001010', ... % -, ., (space), $
'1000100010100010', '1000101000100010', '1010001000100010', '1000101110111010'}; ... % /, +, %, *
strCode39 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*';
idx = strfind(strCode39,upper(in)); % Force to upper case
if any(idx)
result = codesArray{idx};
else
error('Invalid input - character not part of the Code 39 set');
end
end

댓글을 달려면 로그인하십시오.

답변 (2개)

Geoff Hayes
Geoff Hayes 2022년 6월 17일
@Ian - as per the function comment, Encode a single character i. I suspect that you are passing something else i.e. a numeric value like
>> encodeCharacter(42)
Error using encodeCharacter (line 8)
Assertion failed.
Try doing
>> encodeCharacter('4')
ans =
'1010001110101110'
or any other character. Note that you can only pass in one character. If you pass in 2 or more, then a different assert will fire.

Cris LaPierre
Cris LaPierre 2022년 6월 17일
That is what an assert does. If the assertion fails (here that means that in is not a char), then an error is thrown and the message 'Assertion failed.' is thrown.
% Assertion is true -> no error
in = '5';
assert(ischar(in))
% Assertion is false -> error
in = 5;
assert(ischar(in))
Error using assert
Assertion failed.

카테고리

Help CenterFile Exchange에서 Extend Unit Testing Framework에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by