Loop for square matrix

10 views (last 30 days)
Ayanda Ncube
Ayanda Ncube on 13 Sep 2020
Answered: Walter Roberson on 13 Sep 2020
Hi,
I am new to Matlab and have a simple question. How do I write a proram that can read a square matrix from an ACII file to find the element with the maximum value using a loop that reads row by row.

Accepted Answer

Matt J
Matt J on 13 Sep 2020

More Answers (1)

Walter Roberson
Walter Roberson on 13 Sep 2020
In MATLAB, there are a few possibilities.
When you are asked to read row by row, they probably expect that you will use either fgets() or fgetl() and then analyze each input line.
Another way that technically reads row by row is to use textscan() with a format of '' (the empty vector) and a count of 1 immediately after the format. In the context of an assignment, I would only use this if my point were to demonstrate that the assignment was "talking down" to the class.
A third way that technically reads row by row is to use fileread() to read the full file into a character vector, then split it into lines, and loop processing the lines.
Once you have one particular line to focus on, you have several options, including using sscanf(), or str2num() (not recommended for security reasons), or split into fields and use str2double(). Or you could use textscan() on the line.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!