How to validate a text in MatLab? - validatestring -- MatLab

 How to validate a text in MatLab? - validatestring -- MatLab

How to validate a text in MatLab? 
[Ans]
validatestring

[description]
validatestring(s,validateS);
It will check validity of s against validateS.
validateS is valid when only one of validateS is started with s (the matched string is the only one)
, or all of validateS are started with S and any of validateS is the substring of others (the matched string is the shortest length one).
When validateS is valid, validatestring function will throw the matched string.
Otherwise, it will throw exception.

more details on:

code
clear
clc
a = findArea('Rect',10,3,'cm')
fprintf("1\n");
a = findArea('octagon',7,13,'in')
fprintf("2\n");
a = findArea('TRI',10,3,'mi')
fprintf("3\n");
function a = findArea(shape,h,w,units)
expectedShapes = {'square','rectangle','triangle'};
expectedUnits = {'cm','m','in','ft','yds'};
shapeName = validatestring(shape,expectedShapes,1);
unitAbbrev = validatestring(units,expectedUnits,mfilename,'units',4);
switch shapeName
case {'square','rectangle'}
a = h*w;
case {'triangle'}
a = h*w/2;
otherwise
error('Unknown shape passing validation.')
end
end

result:

a =

30

1
Expected input number 1 to match one of these values:

'square', 'rectangle', 'triangle'

The input, 'octagon', did not match any of the valid values.

a = findArea('octagon',7,13,'in')

explanation

In second function call
a = findArea('octagon',7,13,'in')
the validateS {'square','rectangle','triangle'} can not be matched with the s 'octagon' because
validateS are not started with 'octagon' elements-byelements.

In third function call
a = findArea('TRI',10,3,'mi')
the validateS {'cm','m','in','ft','yds'} can not be matched with s 'mi' because
validateS are not started with 'octagon' elements-byelements.

Comments

Popular posts from this blog

How to format Multiple String with formatted data in MatLab? - compose -- MatLab