pythonmatlabcsvmat

Convert the struct to double in MATLAB while reading it from CSV file


I would like to read the CSV file in the MATLAB and slice the array.

The link of the same file in CSV format: https://easyupload.io/kku924

I am reading the CSV file using this code in Python:

import numpy as np
df = np.loadtxt(open("datafile.csv", "rb"), delimiter=",")

The shape of df is (4,12)

output:

array([[ 0.        , -0.01809714, -0.030288  ,  0.00723909,  0.        ,
         0.07616035, -0.00225021,  0.02599635,  0.        , -0.00560653,
        -0.03818442,  0.0650133 ],
       [ 0.08148054,  0.        , -0.01672672, -0.00293869,  0.08233277,
         0.        ,  0.00140302, -0.0524725 ,  0.0555664 ,  0.        ,
        -0.00679785, -0.01236324],
       [-0.00701317,  0.02287405,  0.        ,  0.10824596, -0.0822568 ,
        -0.02795781,  0.        , -0.08929713, -0.04435674,  0.06629118,
         0.        ,  0.01483076],
       [ 0.04419764, -0.01639843,  0.01021535,  0.        , -0.07180185,
        -0.01492615,  0.02212455,  0.        , -0.01258146, -0.00941751,
         0.00876711,  0.        ]])

And I am now able to do these operations:

sub_arr_1 = df[0:4, 0:4]
sub_arr_2 = df[:, 4:8]
sub_arr_3 = df[:, 8:12]

I want to read the same CSV file using MATLAB so that I am able to perform these operations:

 data = readtable('datafile.csv');
 a1 = data(0:4,0:4)
 a2 = data(:,4:8)
 a3 = data(:,9:12)

But I couldn't do it. Can we convert the CSV file to ".mat" and do the same? Or can we read the CSV and slice the matrix in MATLAB itself?


Solution

  • readtable reads a CSV file and outputs a table object. This is equivalent to a Pandas DataFrame object in Python.

    Instead, use readmatrix, which reads a CSV file and outputs a plain numeric matrix.