Loop for square matrix
이전 댓글 표시
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.
채택된 답변
추가 답변 (1개)
Walter Roberson
2020년 9월 13일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!