MATLAB: Code to import csv file which contains text and numbers

csv file text 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

Best Answer

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.
Related Question