MATLAB 04-2 (String)

신예진·2022년 1월 27일
0

Matlab Simulink

목록 보기
7/17

1) string array

s1 = 'I am' % size = 1x4, char 하나가 2byte, class = char
s2 = ' a boy.'
whos s1 s2
abs(s1) % abs(char) shows ascii codes
char(ans) % 다시 char로
s12 = [s1, s2]
s12'
s3 = 'Who are you?'
s4 = [s1,s2;s3] % correct this, * 꿀팁 : whos s1 s2 s3 한 뒤, dimension에 맞게 수정 후 넣어주기
s5 = 'dad's watch' % Error 
s5 = 'dad''s watch' % type ' twice(='') to represent ' as character

2) num2str

num2str(pi) 
num2str(pi,'%.6f') % .6 means 6 digits after decimal point, '3.141593'
num2str(pi,'%.4e') % f = fixed point, e = floating point, '3.1416e+00'

3) combining number and string

i = 35
s = ['answer:', i , 'km/h'] % = 'answer:#km/h', 이렇게 하면 array, scalar, array 인데,
                            % array = a collection of homogeneous data 이기 때문에 'answer:'이 str 여서 s는 str로 인식, 따라서 i를 ascii code 35번째로 인식해 그 숫자에 해당하는 '#'을 뱉음. 
s = ['answer:', num2str(i), 'km/h'] % 이렇게 하면 'answer:35km/h'

0개의 댓글