matlab2 - Introduction to Matlab Creating Matlab Scripts...

Info iconThis preview shows pages 1–18. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Matlab Creating Matlab Scripts Birth Year Program Get the users age as input Display the users birth year Use input() to get the age Use disp to display the year Type in Your Program Give Your Script a Name Run Your Program Program in Action 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; 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) ]); 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)]); 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)]); 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...
View Full Document

This note was uploaded on 10/29/2010 for the course CS 111 taught by Professor Stuff during the Fall '08 term at Bilkent University.

Page1 / 49

matlab2 - Introduction to Matlab Creating Matlab Scripts...

This preview shows document pages 1 - 18. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online