"readmatirx" does not read the expected data?

5 views (last 30 days)
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 Comment
KSSV
KSSV on 15 Nov 2021
Why to specufy posititon? Read whole matrix and pick the required one using indexing.

Sign in to comment.

Accepted Answer

Vedant Shah
Vedant Shah on 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 Comments
Stephen23
Stephen23 on 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 = 1×51
0 1.0000 0.7382 0.5086 0.3818 0.3015 0.2462 0.2058 0.1750 0.1509 0.1316 0.1157 0.1025 0.0914 0.0819 0.0736 0.0665 0.0602 0.0547 0.0498 0.0454 0.0415 0.0380 0.0347 0.0318 0.0292 0.0267 0.0245 0.0225 0.0206
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
M = readmatrix('T2.csv', 'Range', 'B1:AY1')
M = 1×50
1.0000 0.7382 0.5086 0.3818 0.3015 0.2462 0.2058 0.1750 0.1509 0.1316 0.1157 0.1025 0.0914 0.0819 0.0736 0.0665 0.0602 0.0547 0.0498 0.0454 0.0415 0.0380 0.0347 0.0318 0.0292 0.0267 0.0245 0.0225 0.0206 0.0188
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
readmatrix("T2.csv", "Range","A1:E1")
ans = 1×5
0 1.0000 0.7382 0.5086 0.3818
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
readmatrix("T2.csv", "Range","B1:E1")
ans = 1×4
1.0000 0.7382 0.5086 0.3818
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
0.0,1.0,0.738173537690035,0.5085718945625297,0.3818114833781992,0.30152288811815803,0.2461633975711672,0.2057571692109543,0.1750339123047904,0.15094004189111568,0.13158166090227272,0.11572100222240495,0.10251478388290215,0.09136870756110145,0.08185200024083146,0.07364491264917741,0.06650522573304661,0.060246186819108385,0.054721567596416595,0.049815298384133094,0.0454341231180433,0.04150229607214739,0.03795768797503274,0.03474888352185092,0.03183298817969899,0.029173950305950897,0.02674126291374525,0.02450894872671965,0.02245475911045516,0.020559536225688776,0.018806700992361258,0.017181838927628434,0.015672362780666613,0.014267235909965733,0.012956744065336624,0.01173230601377595,0.010586315542220533,0.00951200896266189,0.008503353465953548,0.007554952613749239,0.006661965991776031,0.005820040622406393,0.005025252187590046,0.004274054472562578,0.003563235727400815,0.002889880873406134,0.0022513386666593326,0.001645193081279825,0.0010692382971869499,0.000521456777146142,0.0

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!