Help with readmatrix function

33 次查看(过去 30 天)
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.

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by