Mismatch between file and format character vector when characters are skipped

조회 수: 27 (최근 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
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


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




Community Treasure Hunt

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

Start Hunting!

Translated by