Help with readmatrix function

조회 수: 49 (최근 30일)
John Wood
John Wood 2022년 4월 14일
댓글: Star Strider 2022년 4월 14일
Hello, all. I've written some MATLAB code that results in generation of an m x n matrix of strings like "A1358", "C1457" and so on. I can use the writematrix function to write this data to a .txt file. Examining the .txt file with either Notepad or Excel shows that all values are present. But when I use readmatrix to load the file the first row of data is completely ignored. I used opts=detectImportOptions and readmatrix('filename.txt',opts) to read the data.. All the other data entries using readmatrix are present except for the first row. Can anyone help? Thanks.

채택된 답변

Star Strider
Star Strider 2022년 4월 14일
I’m not certain that I understand what’s actually in the file.
If the first line are headers for each column, use readtable instead. All the headers wil be imported (as variable names), although it can be set to not consider the first row as variable names using 'ReadVariableNames',false. See the documentation for further details and options.
Numeric data can be extracted as a matrix using the table2array function.
  댓글 수: 4
John Wood
John Wood 2022년 4월 14일
OK, this appears to work but only if the detectImportOptions feature isn't used in conjunction. Thanks for all the time. I'm not sure I would've dug this up on my own.
Star Strider
Star Strider 2022년 4월 14일
I didn’t use detectImportOptions because it doesn’t appear to be needed here. I’m sure adding 'OutputType' to the function arguments wouldn’t cause problems, because it’s a display option, not an import option. If you’re having problems with using it with detectImportOptions, post your code. It may be possible to determine if there’s a conflict, and if so, how to resolve it.
Also, if my Answer helped you solve your problem, please Accept it.
.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

John Wood
John Wood 2022년 4월 14일
OK, but I think that should also be a restriction with writematrix as well. I tried using readcell in place of readmatrix but I get the same result. You would think read/write would be one of the simpler MATLAB function calls. Thanks again for taking the time.
  댓글 수: 1
Star Strider
Star Strider 2022년 4월 14일
It would help to have the file to work with. I still suggest that readtable might be best for this.

댓글을 달려면 로그인하십시오.


John Wood
John Wood 2022년 4월 14일
I've attached the file created with writematrix.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by