Reading CSV as Structure Array
72 次查看(过去 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!