Reading CSV as Structure Array
150 views (last 30 days)
Show older comments
Jonathan Josephs-Spaulding
on 26 May 2021
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 Comments
Accepted Answer
Stephen23
on 26 May 2021
Edited: Stephen23
on 26 May 2021
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 Comments
Stephen23
on 27 May 2021
Edited: Stephen23
on 27 May 2021
"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.
More Answers (0)
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!