How to convert some string columns in a csv

8 vues (au cours des 30 derniers jours)
Mohammad Amin Javadi
Mohammad Amin Javadi le 1 Fév 2016
Commenté : Star Strider le 22 Juil 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

Réponse acceptée

Star Strider
Star Strider le 1 Fév 2016
Modifié(e) : Star Strider le 1 Fév 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 commentaires
Agnes Palit
Agnes Palit le 22 Juil 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 le 22 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by