Help with readmatrix function

36 views (last 30 days)
John Wood
John Wood on 14 Apr 2022
Commented: Star Strider on 14 Apr 2022
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.

Accepted Answer

Star Strider
Star Strider on 14 Apr 2022
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 Comments
John Wood
John Wood on 14 Apr 2022
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 on 14 Apr 2022
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.
.

Sign in to comment.

More Answers (2)

John Wood
John Wood on 14 Apr 2022
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 Comment
Star Strider
Star Strider on 14 Apr 2022
It would help to have the file to work with. I still suggest that readtable might be best for this.

Sign in to comment.


John Wood
John Wood on 14 Apr 2022
I've attached the file created with writematrix.

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!