Azzera filtri
Azzera filtri

how to import csv file in matlab

8.533 visualizzazioni (ultimi 30 giorni)
Nur Zakaria
Nur Zakaria il 18 Apr 2013
Commentato: Dyuman Joshi il 29 Dic 2023
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.
  4 Commenti
Pratik il 29 Dic 2023
Spostato: Dyuman Joshi il 29 Dic 2023
I have .csv, in which first column is in date format. second and third columns are integer values. how to read those columns and plot ?
Dyuman Joshi
Dyuman Joshi il 29 Dic 2023
Use readtimetable and specify the 1st column as the date-time data.

Accedi per commentare.

Risposta accettata

Mukesh Jadhav
Mukesh Jadhav il 9 Ott 2016
Modificato: MathWorks Support Team il 15 Mar 2021
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 Commenti
francisco caldeira
francisco caldeira il 4 Mag 2020
Modificato: francisco caldeira il 4 Mag 2020
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
Yongwon Jang il 18 Lug 2023
In ver 2023a, syntex changed like below:
T = readtable('myfile.csv', 'VariableNamingRule', 'preserve');

Accedi per commentare.

Più risposte (3)

Karen Hornsby
Karen Hornsby il 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
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

Thomas Seers
Thomas Seers il 18 Apr 2013
Modificato: Thomas Seers il 18 Apr 2013
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
  1 Commento
Thomas Carter
Thomas Carter il 6 Giu 2022
Modificato: Thomas Carter il 6 Giu 2022
I have found that with large datasets, csvimport corrupts data. Spent a full day trying to figure this one out.

Accedi per commentare.

jgd0008 il 2 Dic 2016
Modificato: per isakson il 2 Dic 2016
Hi, Something like this, may work;
data = fopen('file_name.csv');
A = textscan(data,'%s','Delimiter','\n');
B = A{1,1};
C = textscan(B,'%s','Delimiter',',');
D = C{1};


Scopri di più su Large Files and Big Data in Help Center e File Exchange


Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by