MATLAB: How to convert some string columns in a csv

csvstring to number

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

Best Answer

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.