how to import csv file in matlab
조회 수: 9,731 (최근 30일)
이전 댓글 표시
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.
채택된 답변
Mukesh Jadhav
2016년 10월 9일
편집: MathWorks Support Team
2021년 3월 15일
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','NumHeaderLines',3); % skips the first three rows of data
For more information, see:
댓글 수: 2
francisco caldeira
2020년 5월 4일
편집: 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);
Yongwon Jang
2023년 7월 18일
In ver 2023a, syntex changed like below:
T = readtable('myfile.csv', 'VariableNamingRule', 'preserve');
추가 답변 (3개)
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 Seers
2013년 4월 18일
편집: Thomas Seers
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
댓글 수: 3
Senam
2024년 9월 20일
Please this did not work for me. My matlab could not recognise the csvimport command
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!