How can I read in comma separated data from csv where some cells contain multiple numeric values that are also comma separated?
34 次查看(过去 30 天)
显示 更早的评论
Hi
I have a csv file that I'd like to import into Matlab. It contains a range of Numeric and String data. The function 'readtable' works fine for the most part. The problem I'm having is that the final column you can see below (MouseX) contains comma separated numbers, that when using the readtable function just produces one long number.
To be clear, I don't care about MouseX being in the same table as the rest of the data. I just want a numeric array for MouseX where each current value in a cell has it's own cell. So based on what you can see here, there are 6 cells containing data in MouseX, and each contains 7 comma separated values. So I'd want a 6 x 7 numeric array. It's also worth noting though, that there are a variable number of values in each of those cells.
I've tried reading that colum in separately, using different delimiters, changing from Number to Text etc. The latter kind of worked, in that it produced a string array where the numbers were still comma separated as they appear in the table below. But then I could only think of a clunky loop to extract each value and put it in a numeric array. It took forever to run.
I'm sure there's a more efficient way. I think textscan might be the answer, but I couldn't quite figure it out. There are so many input options! Any help would be much appreciated.
Thanks
Ricci
0 个评论
采纳的回答
ANKUR KUMAR
2021-3-19
编辑:ANKUR KUMAR
2021-3-19
"there are 6 cells containing data in MouseX, and each contains 7 comma separated values. So I'd want a 6 x 7 numeric array.": Ths query has the solution, becuse you can have a matrix of 6X7. But your next statement, "It's also worth noting though, that there are a variable number of values in each of those cells.", if number of values are different in different cells, creating a matrix is not possible unless you are adding summy values in the rows.
csv = readtable("myStopData_MousePos_test_2.csv")
array=csv.MouseX;
array_cell=array(cellfun(@(x) ~isempty(x),array))
array_cell=cellfun(@(x) str2double(strsplit(x,',')),array_cell,'uni',0)
If all cell have the same array size, you can have that in a matrix
cat(1,array_cell{:})
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!