How to convert some string columns in a csv

6 Ansichten (letzte 30 Tage)
Mohammad Amin Javadi
Mohammad Amin Javadi am 1 Feb. 2016
Kommentiert: Star Strider am 22 Jul. 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

Akzeptierte Antwort

Star Strider
Star Strider am 1 Feb. 2016
Bearbeitet: Star Strider am 1 Feb. 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 Kommentare
Agnes Palit
Agnes Palit am 22 Jul. 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 am 22 Jul. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by