필터 지우기
필터 지우기

Why does readmatrix return empty matrix if text file contains quotes?

조회 수: 5 (최근 30일)
Konstantin
Konstantin 2024년 5월 15일
댓글: Cris LaPierre 2024년 5월 17일
Function readmatrix fails to read data from text file when it contains quotes. Why? How to make readmatrix working?
Extended example:
let first text.txt be:
header1
header2
1
2
3
footer
then
readmatrix('text.txt', 'Range', [3 1 5 1])
gives usual vector [1;2;3].
But whe text.txt is like this:
"
header2
1
2
3
"
readmatrix returns empty vector while dlmread works fine.
How to force readmatrix ignore quotes?
  댓글 수: 3
Stephen23
Stephen23 2024년 5월 15일
"Function readmatrix fails to read data from text file when it contains quotes. Why?"
Because double quotes indicate that everything until the next double quote are considered to be one text string. So your sample file consists of exactly one field with some text in it. This is a basic feature of delimited text files (e.g. CSV).
Konstantin
Konstantin 2024년 5월 16일
Thank you for the comments. I also understood that readmatrix seems to treat file with quotes like a single long string. But how to make it ignore the quotes?
readmatrix(filename, 'Range', [range without quotes'] does not work unfortunately.
I have something like hundreds of files with quotes. I of course can preprocess them to remove those, but would be nice to use readmatrx straight away.
I've uploaded test files.

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

답변 (1개)

Cris LaPierre
Cris LaPierre 2024년 5월 15일
readmatrix is trying to guess what the file format is. However it is doing that, it appears to not handle this specific configuation as you might expect. My suspicion is that it is treating everything between the 2 quotes as strings, and readmatrix ignores strings.
You might want to consider reporting this here:
  댓글 수: 1
Cris LaPierre
Cris LaPierre 2024년 5월 17일
Following up, I do not know of a way around this with readmatrix. However, this code seems to be able to import the file with quotes correctly using readtable.
T = readtable('test_quotes.txt','Format','%f','numHeaderLines',2,'readVariableNames',false,'Whitespace','"');
T2 = table2array(T)
T2 = 6x1
1 2 3 4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

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

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by