reading CSV files with multiple rows of strings and multiple rows of floats
6 views (last 30 days)
Show older comments
The first four rows [1:4] of my csv are strings. The remaining rows [5:inf] are numerical data. I need to import all rows, preferably into a table. How can I do this with readtable (or other function). I don't see any way in the opts file. I am probably missing something.
2 Comments
Answers (2)
Steven Lord
on 11 Aug 2025
All the data in a variable in a table must be the same type. You can't have one where the first four rows are text and the next four rows are numbers.
Depending on what you're planning to do, reading the first four rows into a table then reading in the next rows as a separate table, transforming each row of each of those tables into variables using rows2vars, and then concatenating them side-by-side may be suitable for your needs.
t1 = table("abc", "def", "ghi", "jkl", RowNames = "id")
t2 = array2table(magic(4))
t1a = rows2vars(t1)
t2a = rows2vars(t2)
T = [t1a(:, "id"), t2a(:, 2:end)]
0 Comments
See Also
Categories
Find more on Data Import from MATLAB 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!