Code to import csv file which contains text and numbers

Hey everyone,
in an assignment we have to work with a csv file that contains a table with text and numbers. The first row is just text, as it is the description for the columns. From the second row on, you have the data with which you have to work. There are some columns that contain text and other columns that contain numbers.
The file Looks like this:
Name (Col 1) II Country (Col 2) II School (Col 3) II Age in years (Col 4) II Tuition fee (Col5)
So far, I just worked with Excel files, which could be easily imported with the "data = importdata('filename.xlsx')" command. The Goal is to have a Code to automatically Import that file when i run the Code. At the Moment, to write the rest of the Code, I always have to Import the data manually.
Can anyone of you help me with an Import Code?
Thanks in advance for your help!
Best Florian

 Respuesta aceptada

Stephen23
Stephen23 el 14 de Nov. de 2015
Editada: Stephen23 el 14 de Nov. de 2015
You can use textscan, which lets you read text and numeric data from a file. Your useage might be something like this:
fid = fopen(filename,'rt');
C = textscan(fid,'%s%s%s%f%f','HeaderLines',1);
fclose(fif);
Note that there are a lot of choices for textscan, and for its format string, so you will need to read the documentation carefully.
If you actually upload the file then I can show you how to use it for that file. To upload click the paperclip button, then both the Choose file and Attach file buttons.

2 comentarios

I forgot to mention that in the csv file the data is all written in one cell with commas (,) as Separators.
Stephen23
Stephen23 el 14 de Nov. de 2015
Editada: Stephen23 el 14 de Nov. de 2015
Please upload your datafile as I explained in my answer. It is difficult to write a format string for imaginary data that cannot be tested.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 14 de Nov. de 2015

Editada:

el 14 de Nov. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by