Mismatch between file and format character vector when characters are skipped

31 次查看(过去 30 天)
I am getting a "mismatch between file and format character vector" error in dlmread (I know it is a retired function, but I have to be cross-compatible with older versions of matlab). I am reading from a file with both character and numeric data, but I believe I am limiting the dlmread function to only the numeric data. The error mentions the non-numeric data is in 'field number 8', but I believe I am limiting it to columns 0 to 6...
I tried using readtable instead, but it appears to work differently between the two versions of matlab I need to use.
Any help or insight would be appreciated, thanks!
The error is as follows:
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field
number 8) ==> Cnumber
(counts),23,23,25,24,22,21,25,23,25,27,23,24,19,24,24,24,22,17,19,24,23,26,24,26,27,24,23,26,25,25,21,23,22,23,26,25,26,26,24,24,28,27,24,22,24,22,22,23,24,20,20,22,22,21,22,26,24,29,26,25,2...
Error in graphfunction (line 144)
test = dlmread(filename, ',', [1 0 (norows-1) 6]);

采纳的回答

Suvansh Arora
Suvansh Arora 2022-12-20
A workaround is to use either the 'textscan' function or the 'readtable' function. follow the documentation mentioned below for reference:
  1 个评论
Jillian Moffatt
Jillian Moffatt 2023-1-8
Thanks very much for your help; I used the readtable function in the end.
For other's benefit, reading specific lines of the table with the following format:
selection1 = [data{:,1}]; %selection 1
seems to be read by 2017 and 2022 the same way.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2023-1-8
csvread() calls dlmread()
dlmread() calls textscan() passing in a format specification of '' (empty character vector.) That is an undocumented possibility, but in practice it scans the first line (after any headers are skipped) and figures out how many numeric fields there are.
When textscan() with '' is used, there are provisions to skip leading non-text columns, but there are no provisions to skip trailing non-text columns. The way that dlmread() handles a range of columns is to tell textscan() to skip the appropriate number of leading columns, but to read the rest of the line as numeric -- and then dlmread() discards any extra columns. This of course fails if there are any non-numeric columns after the leading columns that have been skipped.
The potential fixes for this include:
  • readtable(), with or without range specification
  • readmatrix() with range specification
  • calling textscan() yourself with a format that uses %*f for skipping numeric columns and %*s for skipping text columns -- or using %*[\n] to skip to end of line
  • or custom processing of the file, such as using fileread() or readlines() and then using pattern matching or regexp() to extract desired data

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by