Matlab 3 - Matlab 3 ASCII Input and Output Tian Gu ID:...

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

View Full Document Right Arrow Icon
1 Matlab 3 ASCII Input and Output Tian Gu ID: 853324472 Team partner: Tielong Su ASCII inLet = input( 'Please enter a letter or single digit number: ' , 's' ); inNum = input( 'Please enter a number: ' ); num =double(inLet); ch = char(inNum); out1 = [ 'The value of ' inLet ' is ' num2str(num) '.' ]; out2 = [ 'The number ' num2str(inNum) ' represent the character ' ch '.' ]; disp(out1); disp(out2); Run the M-file Please enter a letter or single digit number: 6 Please enter a number: 78 The value of 6 is 54. The number 78 represent the character N . (The American Standard Code for Information Interchange is a character-encoding scheme based on the ordering of the English alphabet . ASCII codes represent text in computers , communications equipment, and other devices that use text. Most modern character-encoding schemes are based on ASCII, though they support many more characters than did ASCII.) ASCII explanation from Wikipedia So the ASCII codes represent the letters and digit numbers, and you can see every character by inputing its code. new functions: using help here DOUBLE Convert to double precision. CHAR Create character array (string). NUM2STR Convert numbers ttono a string. Modify the code Require: Example output is shown Please enter a letter: H The value of H is 72. The character L has the value of 76. My M-file inLet = input( 'Please enter a letter or single digit number: ' , 's' ); inLet2 = inLet+4; num =double(inLet); num2 =double(inLet2); out1 = [ 'The value of ' inLet ' is ' num2str(num) '.' ]; out2 = [ 'The character ' inLet2 ' has the value ' num2str(num2) '.' ]; disp(out1); disp(out2);
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Reading from a text file TxtRead.m % This is a comment - that means you can type whatever you want and the % program ignores is. Usually, we use comments to explain what is going % on in the code. To make a comment in MATLAB, just use the % sign % Anything you type on the same line after the % sign is a comment! fid = fopen( 'PlayASCII.txt' , 'r' ); %FOPEN opens a file to access it k = 0; % FEOF checks if the file is at the end or not % The tilde (~) mean not in MATLAB while ~feof(fid) %This says: do what is in the loop while k = k+1; %you are not at the end of the file (PlayASCII) % FSCANF is used to % read in data one % character at a time : data = fscanf(fid, '%c' ,1); if ~isempty(data) %Makes sure the character read in is valid a(k) = data; % the data is stored in 'a' end end disp(a); %Dispaly a fclose(fid); Explanation: open the file named PlayASCII and access it k = 0 Give k a original value. K is used for judging the step. Using while…end to loop the function and k to measure the steps.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 11/17/2011.

Page1 / 13

Matlab 3 - Matlab 3 ASCII Input and Output Tian Gu ID:...

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

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