How to convert some string columns in a csv

4 visualizaciones (últimos 30 días)
Mohammad Amin Javadi
Mohammad Amin Javadi el 1 de Feb. de 2016
Comentada: Star Strider el 22 de Jul. de 2018
Hi I have csv file. It includes 151 rows and each row has 5 columns. The 4 first columns are numbers but the last one is string. How should I convert the last one to a specific number like 1 ? Thanks

Respuesta aceptada

Star Strider
Star Strider el 1 de Feb. de 2016
Editada: Star Strider el 1 de Feb. de 2016
You can read your .csv file with the textscan function without having to convert anything.
Example code:
fidi = fopen(file_name, 'r');
Data = textscan(fidi, '%f%f%f%f%s', 'Delimiter',',', 'CollectOutput',1);
fclose(fidi);
The ‘Data’ variable will be a (1x2) cell, with the first cell containing a (151x4) double array and the second a (151x1) string array.
EDIT — Added fclose call.
  22 comentarios
Agnes Palit
Agnes Palit el 22 de Jul. de 2018
hi, any idea how to solve this?
I have one cell contains the data above on picture. Any idea how to convert that into one table? or one csv file? The type of the data inside the cell is "string"
Star Strider
Star Strider el 22 de Jul. de 2018
The first line is going to be a problem regardless. Normally, I would begin with the readtable function. However, with your file, using the fileread (link) function first, then editing it and converting the rest to a table object would likely be best.
You will have to experiment.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings 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!

Translated by