How can I read in comma separated data from csv where some cells contain multiple numeric values that are also comma separated?
44 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ricci Hannah
el 19 de Mzo. de 2021
Comentada: Ricci Hannah
el 19 de Mzo. de 2021
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/555897/image.png)
0 comentarios
Respuesta aceptada
ANKUR KUMAR
el 19 de Mzo. de 2021
Editada: ANKUR KUMAR
el 19 de Mzo. de 2021
"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{:})
Más respuestas (0)
Ver también
Categorías
Más información sobre Cell Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!