{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# matlab2 - Introduction to Matlab Creating Matlab Scripts...

This preview shows pages 1–19. Sign up to view the full content.

Introduction to Matlab

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Creating Matlab Scripts
Birth Year Program Get the user’s age as input Display the user’s birth year Use input() to get the age Use disp to display the year

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Program in Action

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Search Path
Improve the Birth Year Program The call : “datestr(date, 10)” returns the current year in string format. Rather than the line: “2003 – age”, we could have done: currentYearStr = datestr(date, 10); currentYear = str2num(currentYearStr); birthYear = currentYear – age;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Number guessing program Sample run : Please pick a number between 10 and 99 Now add the two digits of your number Substract the result from your number and enter the result: 36 Now subtract 4 from your number and enter the result: 42 The number you picked was 46
Number Guessing Program % Finds out the two digit number the user is thinking of disp('Please pick a number between 10 and 99'); disp('Now add the two digits of your number'); nineA = input('Substract the result from your number and enter the result: '); A = nineA / 9; nineAoneB = input(['Now subtract ' num2str(A) ... ' from your number and enter the result: ']); B=nineAoneB - 9 * A; disp(['The number you picked was ' num2str(10*A+B) ]); disp(['In other words, it was ' num2str(A) num2str(B) ]);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Computing Digits Given a number and digit number, display the value of that digit. Sample run: Please enter a number 56789 Which digit do you want ? 2 Your digit is : 8
Computing Digits % Gets a number from user and a digit no. starting with 1 % and returns the numeric value of that digit n=input('Please enter a number '); k=input('Which digit do you want ? '); tenToK = 10^k; firstK=rem(n,tenToK); K=floor(firstK/ (tenToK/10)); disp(['Your digit is : ' num2str(K)]);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Getting String Input >> str = input('Enter a string: >', 's'); Enter a string: >so were the days of fall >> disp(['The length of your string was : ' num2str(length(str)) ' characters ...']); The length of your string was : 24 characters ... >> str = input('Enter a string: >'); Enter a string: >having said all that, ??? having said all that, Error: Missing operator, comma, or semicolon. >> s = 'abcde'; >> s(1) ans =a >> s(2) ans =b
Computing Digits, String Version strn=input('Please enter a number ', 's'); k=input('Which digit do you want ? '); location = length(strn) - k + 1; disp(['Your digit is : ' strn(location)]);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
A better guessing program Sample run Multiply your shoe size with 5 and press enter ... Now add 50, press enter when ready ... Now multiply the result with 20 an then add 1006 ... Subtract your birth year from the final result and enter the value >5030 Your shoe size is : 50 Your age is : 30
A better guessing program input('Multiply your shoe size with 5 and press enter ...'); input('Now add 50, press enter when ready ...'); input('Now multiply the result with 20 an then add 1003 ...'); sizeAge = input('Subtract your birth year from the final result and enter the value >'); size = floor(sizeAge/100); age=rem(sizeAge, 100); disp(['Your shoe size is : ' num2str(size) ]); disp(['Your age is : ' num2str(age) ]);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document