6. Input-Output

 

6.1       Copying and pasting data by hand

6.2       Getting input from a user and displaying the result

6.3       Pausing

6.4       Recording reaction time and other delays with tic … toc

6.5       Formatting numbers for screen outputs

6.6       Assigning arrays of literal characters (strings) to variables

6.7       Converting numbers to strings, and concatenating strings

6.8       Comparing strings

6.9       Evaluating strings

6.10     Controlling file print formats

6.11     Writing data to named files

6.12     Checking and changing the current directory

6.13     Reading data saved as plain text

6.14     Reading data from and writing data to Excel spreadsheets

6.15     Taking precautions against overwriting files

6.16     Learning more about input and output

6.17     Practicing input-output

 

top of page

6.1 Copying and pasting data by hand

 

Code 6.1.1

 

my_data = [

          

          ]

 

Output 6.1.1

 

my_data =

 

     []

 

Code 6.1.2

 

my_data = [

1 2 3 4

5 6 7 8

          ]

 

Output 6.1.2

 

my_data =

 

     1     2     3     4

     5     6     7     8

 

 

 

top of page

6.2 Getting input from a user and displaying the result

 

 

Code 6.2.1

 

favorite = input('What is your favorite number? ')

 

Output 6.2.1

 

What is your favorite number?

 

 

Output  6.2.1a

 

What is your favorite number? 3

 

favorite =

 

     3

 

 

Code  6.2.2

 

favorite = input('What is your favorite number between 2 and 7? ')

 

Output  6.2.2

 

??? Undefined function or variable 'p'.

 

Code  6.2.3

 

favorite = -inf;

while (favorite < 2) | (favorite > 7)

    favorite = ...

    input('What is your favorite number between 2 and 7? ')

end

 

Output  6.2.3

 

What is your favorite number between 2 and 7? 88

 

favorite =

 

    88

 

What is your favorite number between 2 and 7? 0

 

favorite =

 

     0

 

What is your favorite number between 2 and 7? 3

 

favorite =

 

     3

 

Code 6.2.4

 

favorite = -inf;

while (favorite < 2) | (favorite > 7)

    favorite = ...

        input('What is your favorite number between 2 and 7? ')

    if (favorite < 2) | (favorite > 7)

        disp('Sorry, is that really the input you intended?')

    end

end

disp(favorite)

 

 

top of page

6.3 Pausing

 

 

Code  6.3.1

 

disp('Hit return to go on.')

pause

 

 

top of page

6.4 Recording reaction times and other delays with tic … toc

 

Code 6.4.1

 

commandwindow

tic

favorite = input('What is your favorite number? ')

RT = toc

 

Output  6.4.1

 

RT =

     2.0830

 

top of page

6.5 Formatting numbers for screen outputs

 

 

Code  6.5.1

 

t = [0:.1:1]';

 

format bank

bank_format_t = t

 

format rat

rational_format_t = t

 

format short

short_format_t = t

 

format short g

short_g_format_t = t

 

format long

long_format_t = t

 

format long g

long_g_format_t = t

 

format   % return format to standard default

standard_format_t

 

Output 6.5.1

 

bank_format_t =

 

             0

          0.10

          0.20

          0.30

          0.40

          0.50

          0.60

          0.70

          0.80

          0.90

          1.00

 

 

rational_format_t =

 

      0     

     1/10   

     1/5    

     3/10   

     2/5    

     1/2    

     3/5    

     7/10   

     4/5    

     9/10   

      1     

 

 

short_format_t =

 

         0

    0.1000

    0.2000

    0.3000

    0.4000

    0.5000

    0.6000

    0.7000

    0.8000

    0.9000

    1.0000

 

 

short_g_format_t =

 

            0

          0.1

          0.2

          0.3

          0.4

          0.5

          0.6

          0.7

          0.8

          0.9

            1

 

 

long_format_t =

 

                  0

   0.10000000000000

   0.20000000000000

   0.30000000000000

   0.40000000000000

   0.50000000000000

   0.60000000000000

   0.70000000000000

   0.80000000000000

   0.90000000000000

   1.00000000000000

 

 

long_g_format_t =

 

                         0

                       0.1

                       0.2

                       0.3

                       0.4

                       0.5

                       0.6

                       0.7

                       0.8

                       0.9

                         1

 

standard_format_t =

 

         0

    0.1000

    0.2000

    0.3000

    0.4000

    0.5000

    0.6000

    0.7000

    0.8000

    0.9000

    1.0000

 

 

top of page

6.6 assigning arrays of literal characters (strings) to variables

 

 

Code 6.6.1

 

name = input('What is your name? ', 's');

 

Output 6.6.1

 

What is your name? David

 

 

Code 6.6.2

 

sprintf('Hello, %s.', name)

 

Output 6.6.2

 

ans =

 

Hello, David.

 

Code 6.6.3

 

piVal = sprintf('The value of %s is %f', 'pi', pi)

 

Output 6.6.3

 

piVal =

 

The value of pi is 3.141593

 

Code 6.6.4

 

int_vs_float = ...

    sprintf('Here is the same number printed as ...

    an integer %d and as a float %f', 3, 3)

 

Output 6.6.4

 

int_vs_float =

 

Here is the same number printed as an integer 3, and as a float 3.000000

 

 

Code 6.6.5

 

twoLines = sprintf('two\nlines')

 

Output 6.6.5

 

twoLines =

 

two

lines

 

Code 6.6.6

 

percent = sprintf('Give 100%%')

 

Output 6.65.6

 

percent =

 

Give 100%

 

 

Code 6.6.7

 

name = input('What''s your name? ', 's');

 

Output 6.6.7

 

What's your name? David

 

Code 6.6.8

 

s = sprintf('ssss');

 

Output 6.6.8

 

 

 

Code 6.6.9

 

s

 

Output 6.6.9

 

s =

 

ssss

 

 

top of page

6.7 Converting numbers to strings, and concatenating strings

 

 

Code 6.7.1

 

f = 7;

disp('Your favorite number is ' f);

 

Output 6.7.1

 

??? Error: File: Number_To_String_01.m Line: 4 Column: 31

Missing MATLAB operator.

 

Code 6.7.2

 

f = 7;

disp(['Your favorite number is ' int2str(f)]);

 

Output 6.7.2

 

Your favorite number is 7

 

 

Code 6.7.3

 

f = 7.2;

disp(strcat('Your favorite number is ', num2str(f)))

 

Output 6.7.3

 

Your favorite number is7.2

 

 

top of page

6.8 Comparing strings

 

Code 6.8.1

 

match_of_aba_and_aba = strcmp('aba', 'aba')

 

match_of_aba_and_abc = strcmp('aba','abc')

 

match_of_aba_and_Aba = strcmp('aba','Aba'’)

 

match_of_lower_aba_and_lower_Aba = strcmp(lower('aba'),lower('Aba'))

 

match_of_upper_aba_and_upper_Aba = strcmp(upper('aba'),upper('Aba'))

 

Output 6.8.1

 

match_of_aba_and_aba =

 

     1

 

 

match_of_aba_and_abc =

 

     0

 

match_of_aba_and_Aba =

 

     0

 

match_of_lower_aba_and_lower_Aba =

 

     1

 

match_of_upper_aba_and_upper_Aba =

 

     1

 

 

top of page

6.9 Evaluating strings and generating numbered variables on the fly

 

Code 6.9.1

 

t = eval('1 + 4')

 

Output 6.9.1

 

t =

 

     5

 

 

Code 6.9.2

 

M1 = [];

M2 = [];

M3 = [];

 

 

Code 6.9.3

 

for i = 1:1028

   

    eval(['M' num2str(i) ' = num2str(i*2)']);

 

end

 

M18

 

Output 6.9.3

 

M18 =

 

     36

 

Code 6.9.4

 

for k = 1:5

    v = genvarname('variable_name_', who);

    eval([v ' = [k*2 k*3 k*4]']);

end

 

% print the value of variable_name_2

j = 2;

eval(['variable_name_' num2str(j)])

 

Output 6.9.4

 

variable_name_ =

 

     2     3     4

 

 

variable_name_1 =

 

     4     6     8

 

 

variable_name_2 =

 

     6     9    12

 

 

variable_name_3 =

 

     8    12    16

 

 

variable_name_4 =

 

    10    15    20

 

 

variable_name_2 =

 

     6     9    12

 

 

 

 

top of page

6.10 Controlling file print formats

 

 

Code 6.10.1

 

fprintf('%s','Matlab can be fun. ');

 

Output 6.10.1

 

Matlab can be fun.

 

Code 6.10.2

 

fprintf('%d',[1:10])

fprintf('\n')

fprintf('%e',[1:10])

fprintf('\n')

fprintf('%f',[1:10])

fprintf('\n')

 

Output 6.10.2

 

12345678910

1.000000e+0002.000000e+0003.000000e+0004.000000e+0005.000000e+0006.000000e+0007.000000e+0008.000000e+0009.000000e+0001.000000e+001

1.0000002.0000003.0000004.0000005.0000006.0000007.0000008.0000009.00000010.000000

 

 

Code 6.10.3

 

fprintf('%6.2f',[1:10])

 

Output 6.10.3

 

  1.00  2.00  3.00  4.00  5.00  6.00  7.00  8.00  9.00 10.00

 

 

Code 6.10.4

 

Pi_matrix = linspace(pi,2*pi,10);

fprintf('\n');

fprintf('%6.0f', Pi_matrix);

fprintf('\n');

fprintf('%6.2f', Pi_matrix);

fprintf('\n');

 

Output 6.10.4

 

    3     3     4     4     5     5     5     6     6     6

  3.14  3.49  3.84  4.19  4.54  4.89  5.24  5.59  5.93  6.28

 

 

Code 6.10.5

 

Pi_matrix

 

Output 6.10.5

 

Pi_matrix =

 

    3.1416    3.4907    3.8397    4.1888    4.5379    4.8869    5.2360    5.5851    5.9341    6.2832

 

 

Code 6.10.6

 

fprintf('%4d',[1:10]);

fprintf('\n');

fprintf('%5d'’,[1:10]);

fprintf('\n');

fprintf('%6d',[1:10]);

 

Output 6.10.6

 

   1   2   3   4   5   6   7   8   9  10

    1    2    3    4    5    6    7    8    9   10

     1     2     3     4     5     6     7     8     9    10

 

 

Code 6.10.7

 

a = [3.1:5.1];

b = [3:5];

c = a*2;

d = b + 2;

 

fprintf('%6.2f',a);

fprintf('%4d',b);

fprintf('\n');

fprintf('%6.2f',c);

fprintf('%4d',d);

fprintf('\n');

 

Output 6.10.7

 

  3.10  4.10  5.10   3   4   5

  6.20  8.20 10.20   5   6   7

 

 

 

top of page

6.12 Checking and changing the current directory

 

Code 6.12.1

 

ls

 

Output 6.12.1

 

fprintf_to_file_1.m                            

mydata.txt 

 

 

Code 6.12.2

 

dir *.m

 

 Output 6.12.2

 

Format_Tester_1.m                   Sort_1.m                           

Format_Tester_2.m                   Sort_2.m                           

Imaginary_Numbers_1.m               Stats_Calculations.m               

Matrix_Calclulations_1.m            Switch_Test_Program_2.m

Mixed_format_fprintf_test_1.m       Timer_1.m                                       

Mixed_format_fprintf_test_2.m       fprintf_to_file_1.m                

Random_Numbers_1.m                 

Simple_calc_1.m                    

Simple_calc_2.m  

 

Code 6.12.3

 

pwd

 

Output 6.12.3

 

ans =

 

C:\Lab and Teach\PSU Teaching\Programming Seminar\Textbook

 

 

Code 6.12.4

 

cd(‘D:\MATLAB of David\’)

pwd

 

Output 6.12.4

 

ans =

 

D:\MATLAB of David

 

 

Code 6.12.5

 

cd(′..′)

pwd

 

Output 6.12.5

 

ans =

 

D:\

 

 

top of page

6.13 Reading data saved as plain text

 

Code 6.13.1

 

data_from_file = load('mydata.txt')

 

Output 6.13.1

 

data_from_file =

 

    1.1000    2.1000    3.1000    4.1000    5.1000

    3.1000    4.1000    5.1000    6.1000    7.1000

 

 

top of page

6.14 Reading data from and writing data to Excel spreadsheets

 

Code 6.14.1

 

M = xlsread('data.xls');

 

Code 6.14.2

 

M = xlsread('data.xls', 'Experiment 2');

 

 

Code 6.14.3

 

xlswrite('My_Excel_File', M);

 

 

top of page

6.15 Taking precautions against overwriting files

 

 

Code 6.15.1

 

filename = input('File name: ', 's');

if ~exist(filename)

    save('filename', 'rows', '-ASCII'’);

else

    disp(['Error: the file '' ' filename ' '' already exists!’]);

end

 

top of page

6.16 Learning more about input and output

 

Code 6.16.1

 

Help iofun

 

Output 6.16.1

 

  File input/output.

 

  File import/export functions.

    dlmread     - Read delimited text file.

    dlmwrite    - Write delimited text file.

    load        - Load workspace from MATLAB (MAT) file.

    importdata  - Load workspace variables disk file.

    wk1read     - Read spreadsheet (WK1) file.

    wk1write    - Write spreadsheet (WK1) file.

    xlsread     - Read spreadsheet (XLS) file.