Code to import csv file which contains text and numbers

5 vues (au cours des 30 derniers jours)
Florian Fbr
Florian Fbr le 14 Nov 2015
Modifié(e) : Stephen23 le 14 Nov 2015
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

Réponse acceptée

Stephen23
Stephen23 le 14 Nov 2015
Modifié(e) : Stephen23 le 14 Nov 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 commentaires
Florian Fbr
Florian Fbr le 14 Nov 2015
I forgot to mention that in the csv file the data is all written in one cell with commas (,) as Separators.
Stephen23
Stephen23 le 14 Nov 2015
Modifié(e) : Stephen23 le 14 Nov 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Text Data Preparation 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