CSV and txt file

7 visualizaciones (últimos 30 días)
Usman Rashid
Usman Rashid el 6 de Nov. de 2018
Respondida: Walter Roberson el 6 de Nov. de 2018
How matlab read and handle csv files and how it read and handle txt file
Can you give me any example where I can extract data from csv file and txt file

Respuestas (1)

Walter Roberson
Walter Roberson el 6 de Nov. de 2018
csvread() calls dlmread() telling it to use delimiter ',' (comma).
dlmread() opens the file and calls textscan(), telling textscan to use that delimiter, and telling textscan() to use the undocumented format '' (empty string).
textscan() with the undocumented format '' (empty string) reads the first line of the file and analyzes it. It breaks it up according to the delimiter, and then counts how many fields are on the line. It constructs a format string that is '%f' repeated that many times. Then it reads the entire file with that format. dlmread() also closes the file afterwards.
Example:
data = csvread('MyFile.csv');
Note: by default, csvread() cannot handle any text in the file. You can pass to csvread() the number of header lines to skip, and the number of leading columns to skip (but not the number of trailing columns to skip.) text is permitted inside those skipped leading rows or leading columns, but will be an error anywhere else.
You can also use xlsread() for csv files. Example:
data = xlsread('MyFile.csv');
If you are on MS Windows with Excel installed, xlsread() will ask Excel to read the file and return the data; if you do not have Excel installed or are on Mac or Linux, xlsread() will call dlmread(). In the case of xlsread() on MS Windows with Excel installed, text is tolerated and is turned into nan, and any leading or trailing all-nan row or column is removed.
You can also use readtable() on csv files. readtable() is able to detect mixes of data types, able to handle strings and sometimes times as well -- especially if you use it together with detectImportOptions:
filename = 'MyFile.csv';
opt = detectImportOptions(filename);
datatable = readtable(filename, opt);

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by