MATLAB Answers

how to import csv file in matlab

11,086 ビュー (過去 30 日間)
Nur Zakaria
Nur Zakaria 2013 年 4 月 18 日
編集済み: francisco caldeira 2020 年 5 月 4 日
Hi, I have one question. I need to import data into MATLAB from a CSV file. Unfortunately, the data has header information in 3 columns.
How do I skip the headers and get the data directly?
For example:
a= import data ('C:\s11.dat') *
Then what is the next step? I need your help.
Thank you.

  1 件のコメント

sehrish
sehrish 2013 年 4 月 18 日
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

サインインしてコメントする。

採用された回答

Mukesh Jadhav
Mukesh Jadhav 2016 年 10 月 9 日
編集済み: MathWorks Support Team 2018 年 11 月 27 日
To import data from a CSV file into MATLAB use the “readtable” function. The “readtable” function automatically detects the header and the number of lines to skip.
T = readtable('myfile.csv');
Alternatively, you can specify the number of lines to skip using:
T = readtable('myfile.csv', 'HeaderLines',3); % skips the first three rows of data
For more information, see:

  1 件のコメント

francisco caldeira
francisco caldeira 2020 年 5 月 4 日
readtable('myfile.csv'); this generates a warning -> ' Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property. Set 'PreserveVariableNames' to true to use the original column headers as table variable names. '
To solve do:
T = readtable('myfile.csv','PreserveVariableNames',true);

サインインしてコメントする。

その他の回答 (4 件)

Karen Hornsby
Karen Hornsby 2013 年 4 月 18 日
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 件のコメント

サインインしてコメントする。


Thomas
Thomas 2013 年 4 月 18 日
編集済み: Thomas 2013 年 4 月 18 日
I think the easiest way is to use CSVIMPORT from the File Exchange:
%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 件のコメント

サインインしてコメントする。


jgd0008
jgd0008 2016 年 12 月 2 日
編集済み: per isakson 2016 年 12 月 2 日
Hi, Something like this, may work;
data = fopen('file_name.csv');
A = textscan(data,'%s','Delimiter','\n');
B = A{1,1};
fclose(fid);
C = textscan(B,'%s','Delimiter',',');
D = C{1};

  0 件のコメント

サインインしてコメントする。


sharmila kanagaraj
sharmila kanagaraj 2017 年 1 月 28 日
anyboady known dynamic clustering how to load dataset in it

  0 件のコメント

サインインしてコメントする。

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by