% Alessio Sacco % 2/16/22 %Basic input and output and text representation %Text data words = 'Good morning!'; disp(words); %Get numeric values of characters % ASCII asNumber = double(words); disp(asNumber); %Get characters from numeric values asChars = char(asNumber); disp(asChars); sort(words); %Build strings just from numbers SLU = [83 76 85]; char(SLU) %% %Anything we can do with numbers, we can do %with characters aChar = 'a'; char(aChar - 5) char(aChar + 5) %% %Converting numbers to strings stringWithNum = '83'; num = str2num(stringWithNum); %integer x = 83; str = int2str(x); % float pi str = num2str(pi); str = num2str(pi,9); %This is an error- returns [] %str2num( 'forty-five' ) %% %Formatted output with fprintf() low = 1; high = 10; % ask the user to write a value between low and high %Clunky way: disp('Write a value between'); disp(low); disp(' and '); disp(high); %% %Elegant solution: low = 1; high = 20; %fprintf % recommended fprintf('Write a value between %d and %d\n',low, high); % not recommended disp('Write a value between 1 and 20'); % integers -> %d % floating-point -> %f % character string -> %s % firstName = 'Alessio'; lastName = 'Sacco'; fprintf('My name is %s %s\n', firstName, lastName); fprintf('The value of pi is: %.2f\n', pi); %For homework 4: year = 2008 month = 5 day = 29 value = 20 fprintf('The stock had value %d on %d/%d/%d\n', value, year, month, day); %% % read this number x = input('Pick a number','s');