Reading CSV as Structure Array
조회 수: 73 (최근 30일)
이전 댓글 표시
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
채택된 답변
Stephen23
2021년 5월 26일
편집: Stephen23
2021년 5월 26일
T = readtable('Test_Expression.csv', 'Decimal',',', 'Delimiter',';')
S = struct('gene',{T.gene}, 'value',T.value)
S.rawValue = [T.rawValue_1, T.rawValue_2, T.rawValue_3, T.rawValue_4]
댓글 수: 5
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개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!