Agenda 6 September 2013 Arrays/Strings

%

% Test1: Wednesday, 18th (online) and Friday 20th (hard copy only)

% Final Exam: C: Wednesday, 11 DEC 6pm

D: Thursday, 12 DEC 6pm

% Review Arrays

% Concept: Strings

% Clickers, end of class (hopefully)

Array

*****Example******

clc

clear

A = ones(10);

B = zeros(4);

C = A;

C(3:6, 3:6) = B

*****Example Arrays ******

clc

close all

car = imread('gray_car.jpg');

% car1 = car

imshow(car)

%car(size)

wtlc = 77;

wtlr = 80;

wbrc = 101;

wbrr = 109;

ptlc = 144;

ptlr = 95;

pbrc = 168;

pbrr = 124;

% make the wheel black

%car(wtlr:wbrr, wtlc:wbrc) = 0;

%car(ptlr:pbrr, ptlc:pbrc) = 255;

car(ptlr:pbrr, ptlc:pbrc) = car(wtlr:wbrr, wtlc:wbrc);

imshow(car)

car2 = car < 100 % dark shade

imshow(car);

figure

imshow(car(end:-1:1,:)) %flip car

figure

imshow(car2);

car3 = car > 100 % op of dark

figure

imshow(car3);

figure

car4 = car2 + car3; % = 100

imshow(car4)

% STRINGS

%

% ASCII Codes – See Appendix B

% American Standard Code for Information Interchange

% Number Representation See Appendix C

% manipulating strings

% casting

% useful functions:

% strtok

% str2num

% num2str

% never submit files containing calls to:

% input

% fprintf -

%

%

% Math with strings

% Token – a collection of characters

% Tokenizing – giving meaning to tokens

% Casting – process of changing the way a language

% views data without changing the meaning of the data

******* Strings **********

clear

clc

str = 'abcd''ABCD' % Note the ‘ needed another ‘; 1X9 array

ascii = double(str); % cast to double, don’t think as string

next = str + 1 % What? Because 1 is a number

next = char(str + 1); % want next character not numbers

strr = '1234' % ASCII chars

num = str2num(strr) % now double

num1 = 8976.5

str1 = num2str(num1)

% Capitalize all letters

str = 'now, is... the time for all good...'

srtASCII = double(str)

strCap = str - 32

strCap = char(str-32)

% Capitalize all words

str = 'now, is... the time for all good...'

% find the spaces

spaces = (str == ' ')

where = find(spaces)

firstLetts = [1 (where+1)]; % Must account for first word

str(firstLetts) = str(firstLetts) + 'A' - 'a'

% How do we find tokens? Or Each word?

% What is a delimiter? A character used to separate tokens

[token1 rest] = strtok(str, ' ,.;?')

[token2 rest] = strtok(rest, ' ,.;?')

[token3 rest] = strtok(rest, ' ,.;?')

% How can we add numbers that are strings?

str = '123.4'

num = str2num(str)

num = num + 3000

nstr = num2str(num)

% INPUTS and OUTPUTS

%

>> fred = 'Fred'

>> n = input('Enter a Number: ')

Enter a Number: 5

% OUTPUTS

% fprintf(....) and sprintf(...)

clc

clear

a = 42;

b = 'fried okra';

n = fprintf('the answer is %d\n cooking %s', a,b);

%s = sprintf('the answer is %d\n cooking %s', a,b)