Reading CSV as Structure Array

조회 수: 73 (최근 30일)
Jonathan Josephs-Spaulding
Jonathan Josephs-Spaulding 2021년 5월 26일
편집: Stephen23 2021년 5월 27일
Dear MATLAB Community,
I am attempting to transform a CSV into a MATLAB structure for input into another function. At present I have come up with:
expression = table2struct(readtable('sample_Expression.csv'))
expression =
136x1 struct array with fields:
gene
rawValue_1
rawValue_2
rawValue_3
rawValue_4
rawValue_5
value
However, I am more interested in creating something like this:
expression =
struct with fields:
gene: {136x1 cell}
rawValue: [136x5 double]
value: [136x1 double]
What is the difference in terms of using the two different structures? Could someone suggest a way to modify my present script to create something in the second example?
Thanks for any help!
  댓글 수: 2
Monika Jaskolka
Monika Jaskolka 2021년 5월 26일
Can you attach your CSV file?
Jonathan Josephs-Spaulding
Jonathan Josephs-Spaulding 2021년 5월 26일
Hi Monika,
Here is a CSV file for your reference, thanks for any help or suggestions!

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

채택된 답변

Stephen23
Stephen23 2021년 5월 26일
편집: Stephen23 2021년 5월 26일
T = readtable('Test_Expression.csv', 'Decimal',',', 'Delimiter',';')
T = 122×7 table
gene rawValue_1 rawValue_2 rawValue_3 rawValue_4 rawValue_5 value ____________________________ __________ __________ __________ __________ __________ _____ {'Chromosome_336:2795' } 258 23 1 184 61 105.4 {'Chromosome_2800:3723' } 175 8 0 65 17 53 {'Chromosome_3733:5016' } 230 7 0 68 14 63.8 {'Chromosome_22934:23872' } 131 39 0 49 13 46.4 {'Chromosome_23918:26731' } 600 170 1 168 37 195.2 {'Chromosome_30266:31084' } 85 2 0 11 1 19.8 {'Chromosome_99529:100836' } 486 117 0 96 29 145.6 {'Chromosome_103207:104658'} 391 95 1 72 25 116.8 {'Chromosome_108999:109910'} 1306 246 4 381 139 415.2 {'Chromosome_196798:197514'} 159 65 0 78 21 64.6 {'Chromosome_198322:199509'} 86 34 0 40 2 32.4 {'Chromosome_200478:201332'} 695 253 2 202 54 241.2 {'Chromosome_205772:206794'} 653 124 0 101 43 184.2 {'Chromosome_207361:208146'} 456 100 0 85 31 134.4 {'Chromosome_233915:234715'} 11 7 0 6 0 4.8 {'Chromosome_293697:294947'} 50 3 0 1 0 10.8
S = struct('gene',{T.gene}, 'value',T.value)
S = struct with fields:
gene: {122×1 cell} value: [122×1 double]
S.rawValue = [T.rawValue_1, T.rawValue_2, T.rawValue_3, T.rawValue_4]
S = struct with fields:
gene: {122×1 cell} value: [122×1 double] rawValue: [122×4 double]
  댓글 수: 5
Jonathan Josephs-Spaulding
Jonathan Josephs-Spaulding 2021년 5월 27일
Hey @Stephen Cobeldick, when I include
% 'Decimal',',',
while reading the table in, I receive the error:
Error using readtable (line 216)
Invalid parameter name: Decimal.
However when this is removed, the answer that you provided works fine. The finally structure still remains as,
S =
struct with fields:
gene: {122x1 cell}
value: [122x1 double]
rawValue: [122x4 double]
Is there a difference here that I should be concerned with? Thanks for your advice!
Stephen23
Stephen23 2021년 5월 27일
편집: Stephen23 2021년 5월 27일
"Is there a difference here that I should be concerned with?"
That depends on your data file: what decimal radix sign does your file actually use?
Did you actually look at the data values (not just at the "122x1 double"), to check if the complete decimal fraction was imported correctly? If the radix symbol does not match the file format, then it is possible that you will only get the integer portion (or something similar, I have not tried this).
Tip: do NOT open and save CSV files (or any text files) using Excel, unless you want to change the format and data of your files in unpredictable ways:
I answer a lot of questions on this forum where the user is not aware that they are working with two different file formats: one saved by some measurement device, another after they open&save the file using Excel.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by