1. Input in MATLAB >> a=input('number '); number 10 % the strings inside number is printed and cursor waits for the value of a a=10 ----------------- >> a=input('number '); number 'abc' % the value of a is a strings quotation marks required a= abc >> a=input('number ','s'); number abc % quotation marks not needed ; input was told to expect a string with 's' a= abc 2. and Output in MATLAB string/integer/num conversions Strings functions (additional material) int2str num2str str2num int2str : integer to string num2str : integer or real to string str2num : string to number (integer or real) >> a=10; >> b=20; >> disp(a) 10 >> disp(b) 20 >> disp(a,b) ??? Error using ==> disp Too many input arguments. >>pi % default format is format short (4 decimal digits) ans = 3.1416 >> format long % 14 decimal digits >>pi ans = 3.14159265358979 >> disp([a b]); % group into a row vector 10 20 clear;clc; x=10; y='5'; z=10; whos Name Size Bytes Class Attributes x 1x1 8 double y 1x1 2 char z 1x1 8 double disp(x); 10 disp(y); 5 disp(z) 10 disp([x z ] ) 10 10 disp([num2str(x) y num2str(z) ] ) 10510 disp([num2str(x) ' ' y 'hello ' num2str(z) ] ) 10 5hello 10 whos x Name Size Bytes Class Attributes x 1x1 8 double 3. and more output with format >> format long e % Exponential notation e+00 mean 10**00 >> pi ans = 3.141592653589793e+00 >> format long g % Best of e or plain >> pi ans = 3.14159265358979 >> format short eng % exponent is multiple of 3 >> format bank >> format hex >> format rat 4. Strings and output : String into Integer/Number conversion and (other way around) >> x=12; >> y=1.2; >> whos x 1x1 8 double array y 1x1 8 double array >>a=int2str(x); >>b=num2str(x); >> whos x 1x1 8 double array y 1x1 8 double array a 1x2 4 char array b 1x2 4 char array >> clear >> s = '123456.7890' >> t= str2num(s); >> whos s 1x11 22 char array t 1x1 8 double array k='12.1234000000000000000000000000000000000000000000000000000' k = 12.1234000000000000000000000000000000000000000000000000000 m=str2num(k); whos k m Name Size Bytes Class Attributes k 1x58 116 char m 1x1 8 double a= [ 'a' 'b' 'c']; b= [ 'abc'] b = abc c = 'abc'; clc a = [ 'a' 'b' 'c']; b = [ 'abc']; c = 'abc'; a a = abc b b = abc c c = abc whos Name Size Bytes Class Attributes a 1x3 6 char b 1x3 6 char c 1x3 6 char a=int8(5); b=5; whos Name Size Bytes Class Attributes a 1x1 1 int8 b 1x1 8 double c=int2str(a); d=int2str(b); who Your variables are: a b c d whos Name Size Bytes Class Attributes a 1x1 1 int8 b 1x1 8 double c 1x1 2 char d 1x1 2 char d d = 5 f=12.123400000000000000000000000000000000; whos Name Size Bytes Class Attributes a 1x1 1 int8 b 1x1 8 double c 1x1 2 char d 1x1 2 char f 1x1 8 double g=num2str(f); whos Name Size Bytes Class Attributes a 1x1 1 int8 b 1x1 8 double c 1x1 2 char d 1x1 2 char f 1x1 8 double g 1x7 14 char g g = 12.1234 f=-12.123400000000000000000000000000000000; g=num2str(f); Name Size Bytes Class Attributes a 1x1 1 int8 b 1x1 8 double c 1x1 2 char d 1x1 2 char f 1x1 8 double g 1x8 16 char g g = -12.1234 k='12.123400000000000000000000000000000000000000000'; l=str2num(k); whos k l Name Size Bytes Class Attributes k 1x48 96 char l 1x1 8 double clear clc EXAMPLES >> a = [ 10 20; 30 40]; >> a a = 10 20 30 40 disp(a) 10 20 30 40 b = [ 100 ; 200] b = 100 200 disp(a,b) Too many input arguments. disp([ a b ] ) 10 20 100 30 40 200 sub4ex1 Enter aa= 100 Enter bb= 200 Result of adding aa to bb is 300 type sub4ex1.m % sub4ex1.m aa=input('Enter aa= '); bb=input('Enter bb= '); cc= aa+bb; disp('Result of adding aa to bb is'); disp(cc); sub4ex1 Enter aa= [ 1 2 ; 3 4 ] Enter bb= [ 100 100 ; 200 200] Result of adding aa to bb is 101 102 203 204 sub4ex1 Enter aa= 'abc' Enter bb= 'defg' Error using + Matrix dimensions must agree. cc= aa+bb; sub4ex1 Enter aa= 'abc' Enter bb= 'def' Result of adding aa to bb is 197 199 201 aa aa = abc bb bb = def whos Name Size Bytes Class Attributes a 2x2 32 double aa 1x3 6 char b 2x1 16 double bb 1x3 6 char cc 1x3 24 double aa+0 ans = 97 98 99 bb+0 ans = 100 101 102 type sub4ex2.m % sub4p9.m <---- change it into sub4ex2.m a=input('Enter scalar ') b=input('Enter row vector ') c=input('Enter string (no quotation marks) ', 's') d=input('Enter string ') clear aa = input('give me a string ' ) give me a string 'abc''d' aa = abc'd aa=input('give me a string','s') give me a stringaa=input('give me a string ','s') give me a string abc'd aa = abc'd clear format long pi ans = 3.141592653589793 format short pi ans = 3.1416 format long e pi ans = 3.141592653589793e+00 10 ans = 10 10000000 ans = 10000000 10000000.123 ans = 1.000000012300000e+07 format long eng ans ans = 10.0000001230000e+006 format bank ans ans = 10000000.12 format rat pi ans = 355/113 1.5 ans = 3/2 format hex pi ans = 400921fb54442d18 10 ans = 4024000000000000 int8(10) ans = 0a int8(20) ans = 14 clear pi ans = 400921fb54442d18 clear format short pi ans = 3.1416