"readmatirx" does not read the expected data?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
cui,xingxing
am 15 Nov. 2021
Kommentiert: cui,xingxing
am 3 Feb. 2025
My data is 1×51 size in csv, when I use readmatrix function to specify "Range" to import 1 to 50 data, it is empty, or import other range data, the result is all data, my sample code is as follows, thanks in advance! (The "T2.csv" is in the attachment)
T2 = readmatrix("T2.csv","Range","B1:AY1") % empty ???
T2 =
[]
T2 = readmatrix("T2.csv","Range","A1:E1") % all data is imported ???
T2 =
Columns 1 through 12
0 1.0000 0.7382 0.5086 0.3818 0.3015 0.2462 0.2058 0.1750 0.1509 0.1316 0.1157
Columns 13 through 24
0.1025 0.0914 0.0819 0.0736 0.0665 0.0602 0.0547 0.0498 0.0454 0.0415 0.0380 0.0347
Columns 25 through 36
0.0318 0.0292 0.0267 0.0245 0.0225 0.0206 0.0188 0.0172 0.0157 0.0143 0.0130 0.0117
Columns 37 through 48
0.0106 0.0095 0.0085 0.0076 0.0067 0.0058 0.0050 0.0043 0.0036 0.0029 0.0023 0.0016
Columns 49 through 51
0.0011 0.0005 0
T2 = readmatrix("T2.csv","Range","B1:E1") % also empty ???
T2 =
[]
The results of the above experiment are very strange, but the import of data from "range" using the "xlsread" function is normal
1 Kommentar
KSSV
am 15 Nov. 2021
Why to specufy posititon? Read whole matrix and pick the required one using indexing.
Akzeptierte Antwort
Vedant Shah
am 3 Feb. 2025
Upon analysing the attached .csv file, I noticed that the data is separated by commas (,), whereas the default delimiter used in the “readmatrix” function is a semicolon (;). Since the delimiter was not specified as a comma, it gave unexpected outcomes, such as importing all data or returning an empty array.
To determine the delimiter used in any file, you can open the file as text and observe how the data is separated.
By specifying the delimiter as a comma in the code, we can achieve the desired results. The modified code is as follows:
T2 = readmatrix("T2.csv", 'Range', 'B1:AY1', 'Delimiter', ',');
T2 = readmatrix("T2.csv","Range","A1:E1", 'Delimiter', ',');
T2 = readmatrix("T2.csv","Range","B1:E1", 'Delimiter', ',');
This will provide the desired output as below:

For more information about delimiters, you can refer the following “readmatrix” documentation:
2 Kommentare
Stephen23
am 3 Feb. 2025
"I noticed that the data is separated by commas (,), whereas the default delimiter used in the “readmatrix” function is a semicolon (;)"
No, this is incorrect. READMATRIX does not have a default delimiter, it actually automagically determines the delimiter from the file content. The READMATRIX states "The readmatrix function performs automatic detection of import parameters for your file", which includes detecting the delimiter character.
M = readmatrix('T2.csv')
M = readmatrix('T2.csv', 'Range', 'B1:AY1')
readmatrix("T2.csv", "Range","A1:E1")
readmatrix("T2.csv", "Range","B1:E1")
Given that the file extension CSV stands for "Comma-separated values" and the comma is by far the most common separator character in the USA where MATLAB is developed and sold, not handling commas (as this answer incorrectly claims) would be a very very poor design decision. Of course there are certainly cases where the file content may confuse the heuristics used to automgically determine the delimiter, but I doubt that this is the case for this very simple file content:
type T2.csv
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!