Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how to import csv file in matlab

Asked by Nur Zakaria on 18 Apr 2013

Hi.

I have one question. Actually, data that i have in csv file. So, i need to import data to Matlab. Unfortunately, the data has header in 3 column.

how to skip to take the data directly?

for example:

a= import data ('C:\s11.dat') *

then what the next step? Need your help.

Thank you.

1 Comment

sehrish on 18 Apr 2013

As you click on import data there is option "import as"..there are three options to save data as a Matrix OR Column OR Cell array So if u want to save it as a whole thn select matrix and ur whole variable will be saved in a variable if u want wise then select Column option and data will be saved in column and variables will be shown in work space

Nur Zakaria

Tags

No tags are associated with this question.

Products

No products are associated with this question.

2 Answers

Answer by Karen Hornsby on 18 Apr 2013

HI, You can either use the import data wizard by right clicking on the file in the current folder window. When the import wizard opens it should give you a preview of the data and in the top right is a box which asks you how many header lines there are. You can use this to create code to open files of this type repeatedly (this works well but if your new to matlab it can be a bit confusing to edit) or you can used the following code to open files

ftoread = '%file name';
fid = fopen(ftoread);
fgetl(fid) %reads line but does nothing with it
fgetl(fid)
fgetl(fid)
M = textscan(fid, '%f', 'Delimiter','\,'); % you will need to change the number   of values to match your file %f for numbers and %s for strings.
fclose (fid)

You can get more help with this in the help file, just type in the command you want help with in the search box. Karen

0 Comments

Karen Hornsby
Answer by Thomas on 18 Apr 2013
Edited by Thomas on 18 Apr 2013

I think the easiest way is to use CSVIMPORT from the File Exchange:

http://www.mathworks.co.uk/matlabcentral/fileexchange/23573-csvimport

%read data example: Import columns as column vectors 
[X Y Z] = csvimport('vectors.csv', 'columns', {'X, 'Y', 'Z'});
%remove headers
X(1) = [];
Y(1) = [];
Z(1) = [];

This assumes that the first element in the array contains the header

Thomas

0 Comments

Thomas

Contact us