How to read data from a CSV into a column vector

조회 수: 65 (최근 30일)
waleed khalid
waleed khalid 2020년 11월 5일
댓글: Monika Jaskolka 2020년 11월 5일
I have 25 csv files that I need to grab data from, they are all in the same format with no headers, I would like to grab all of this data, and store it into a column vector. Previous iterations of this program have used fread(filename, size) to read binary files into a column vector and throw them into a calculation script, I would like to keep the script in the same format so I need to read this csv data into a Column Vector.
I also imported them into a table so if there is a way to convert this table into a Column vector that works too, I am not concerned with the loss of efficiency in terms of runtiime.

답변 (1개)

Monika Jaskolka
Monika Jaskolka 2020년 11월 5일
편집: Monika Jaskolka 2020년 11월 5일
>> m = readmatrix('test.csv')
m =
1 2 3 4
5 6 7 8
>> m = m(:)
m =
1
5
2
6
3
7
4
8
For mixed data:
>> m = readtable('test.csv')
m =
2×4 table
Var1 Var2 Var3 Var4
____ ____ _____ ____
1 2 {'a'} 4
5 6 {'b'} 8
>> m = table2cell(m)
m =
2×4 cell array
{[1]} {[2]} {'a'} {[4]}
{[5]} {[6]} {'b'} {[8]}
>> m = m(:)
m =
8×1 cell array
{[1]}
{[5]}
{[2]}
{[6]}
{'a'}
{'b'}
{[4]}
{[8]}
  댓글 수: 4
waleed khalid
waleed khalid 2020년 11월 5일
Thanks for that! For some reason, when I call readmatrix(filename), the values in my csv's become obfuscated to this:
0.0000 0.0000 0.0000 -0.0000 0.0002 0.0000
0.0000 0.0000 0.0000 -0.0000 0.0002 0.0000
0.0000 0.0000 0.0000 -0.0000 0.0002 0.0000
0.0000 0.0000 0.0000 -0.0000 0.0002 0.0000
0.0000 0.0000 0.0000 -0.0000 0.0002 0.0000
Monika Jaskolka
Monika Jaskolka 2020년 11월 5일
The data is there, but Matlab has different formats for displaying data in the Command Window. Read more about it here: https://www.mathworks.com/help/matlab/ref/format.html . The default is "format short", which is only 4 decimal places.
So if you want to see more decimals, type in "format long" into your Command Window, and then try to display the matrix again.

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by