Question

There is a file called mat2.txt in our files area under the Assignments folder. Download it...

There is a file called mat2.txt in our files area under the Assignments folder. Download it and save it in the same folder where this Matlab file (HW08_02.m) is saved. It may be worth opening that text file to see how it is set out. Note well, however, that the file might have a different number of lines and different number of numbers on each line. Write a Matlab program that does the following:

Prompt the user for an input file name and read the user's input to a string variable. Do not assume that the file name will be mat2.txt. (In other words, your script should run fine if the user enters a different file name that has data in the same format.)

Open the file name the user has entered for reading and read the first line to see how many lines and columns it has. Use that information to set up a loop to read the rest of the input file into an array. Close the input file.

Use a separate loop to print the array to the console in a neat table. Noting that none of the numbers in the input file is larger than 99, choose an output format that results in a neat table with no more than 4 spaces before each number.*

Do not assume that the input file has a fixed number of rows or columns. Assume that that information is on the first line of the file and read it. Your program may be tested with a different file with a different number of rows and columns.

* Note that this will require some thought, since the number of columns is can change and your code must adjust for that at runtime. There are several different ways to do this. Among them are creating the format string dynamically as in 2d or using a nested loop to print one column at a time on an output line.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

\color{blue}\underline{mat2.m:}

filename = input("Enter input file",'s');

fid=fopen(filename);
rows = fscanf(fid, '%d', 1);
fscanf(fid, '%s', 1);
columns = fscanf(fid, '%d', 1);

mat = [];

% Reading data from file and puting data into 2D mat
for i = 1:rows-1
for j=1:columns
mat(i,j)=fscanf(fid, '%d', 1);
end
end

disp(mat);
fclose(fid);

\color{blue}\underline{mat2.txt:}

10, 5
75 82 48 49 95
74 69 44 95 54
39 31 64 34 13
65 95 70 58 14
17 3 75 22 25
70 43 27 75 84
3 38 67 25 25
27 76 65 50 81
4 79 16 69 24
9 18 11 89 92

\color{red}\underline{Output:}

Add a comment
Know the answer?
Add Answer to:
There is a file called mat2.txt in our files area under the Assignments folder. Download it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Assume that you will be given a *.txt file, which in the same format of RandSource.txt...

    Assume that you will be given a *.txt file, which in the same format of RandSource.txt (attached). The file has three numbers at each line. Numbers are separated with a space. All number are in the range of [0, 50]. The first two numbers indicate a range. The third one shows number of random numbers to be generated in the range of first two numbers. The range always given in first < second order. If not, raise an error. For...

  • c++ question, i put the txt attachment picture for this question... please include comments on calculations...

    c++ question, i put the txt attachment picture for this question... please include comments on calculations and varibles Description In this program, you will write a program to emulate a vending machine (somewhat). In this version of the program, you will use a struct type defined below struct snackType string name; string code; double price; int remaining; Where name contains the snack's name, code contains the 2 digit code for the item, price holds the item's price, and remaining holds...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Create a java project. Create a class called cls2DarrayProcessor.java. with the following: Reads numbers from a...

    Create a java project. Create a class called cls2DarrayProcessor.java. with the following: Reads numbers from a pre-defined text file. A: Text file name should be data.txt B: Text file should be in the same workspace directory of your project's folder C: Text file name should be STORED in a String and called: String strFile ='./data.txt' Defines a 2-D array of integers with dimensions of 5 rows by 5 columns. Inserts the data from the text file to the 2-D array...

  • Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns...

    Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns 1-4 6-8 number of a student A grade out of 100 EYERCISE 12-4 Write a program that will read the identification numbers of students and their letter grades (A, B, C, D, or F) from the keyboard and store them in a text file...

  • Write a C++ that read a list of numbers from a .txt file into array. The...

    Write a C++ that read a list of numbers from a .txt file into array. The first digit on the list will be array size, while the second number on the list will be our first number in our array list. Example: t1.txt file contain list off input number: 5, 3, 6, 10, 43, 23. notice the first number is 5 which will need to be use as an array size and the rest of the numbers are in the...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • 12.8 GPA reports using files This program is to compute and write to a file the...

    12.8 GPA reports using files This program is to compute and write to a file the GPA for student scores read from an input file. As in Lab 7.5, the point values of the grades are 4 for A, 3 for B, 2 for C, 1 for D, and 0 for F. Except for the grade A, where + has no effect, a + after the letter increases the point value by 0.3, and except for F, where a -...

  • ****PLEASE CODE IN C++ and line doc explaining what is happening!***** **HERE IS THE INPUT FILE(.txt)*****...

    ****PLEASE CODE IN C++ and line doc explaining what is happening!***** **HERE IS THE INPUT FILE(.txt)***** Ottawa Senators New York Rangers Boston Bruins Montreal Canadiens Montreal Canadiens Toronto Maple Leafs New York Rangers Chicago Blackhawks Montreal Maroons Detroit Red Wings Detroit Red Wings Chicago Blackhawks Boston Bruins New York Rangers Boston Bruins Toronto Maple Leafs Detroit Red Wings Montreal Canadiens Toronto Maple Leafs Montreal Canadiens Toronto Maple Leafs Toronto Maple Leafs Toronto Maple Leafs Detroit Red Wings Toronto Maple Leafs...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT