Exercises

Please complete each exercise or answer the question before reviewing the posted solution comments.

Use the clear command before starting your work on a new problem to ensure that any existing variable name assignments don't interfere with your work on the new problem.

    1. Create a file data1.txt with a row of numeric values.
    2. Create a second file data2.txt with a column of numeric values.
    3. Load each file into a new script using a different technique.
    4. Use the disp command to display somethings similar to this output for each file you create.
      data1.txt has 10 values sum=239757 avg=23975.7
      data2.txt has 9 values sum=12035 avg=1337.2222

    Here's an example of the code that will process data file 1. Copy, paste and edit for your other file. Be sure to try a different use of the load command for the second file.

    file1 = 'data1.txt';
    data1 = load( file1 );
    
    % Display the number of values, the sum and the average for file1
    count1 = length(data1);
    sum1 = sum(data1);
    avg1 = 0;  % default average value if no data in the file
    if count1 > 0
        avg1 = sum1 / count1;
    end   
    disp([file1 ' has ' num2str(count1) ' values sum=' num2str(sum1) ' avg=' num2str(avg1)]);
    
  1. What is the output of the above script if the data file contains this matrix of values?

    234 234 1231 526 23
    214 522 5235 13 325
    124 123 346 8 87679

    Here are the results for a data file with three rows and five values per row. Remember that length returns the larger of the two dimensions.

    data.txt has 5 values sum=572 879 6812 547 88027  avg=114.4 175.8 1362.4 109.4 17605.4
    

    Note: The output has been modified slightly to fit in this screen frame.