Azzera filtri
Azzera filtri

How can I import data from .csv file with numeric values and texts (with column headers) into MATLAB Workspace?

322 visualizzazioni (ultimi 30 giorni)
I have data files (.csv of course) which contains several columns each with a column header. I want to import the data as I do in R. Using 'csvimport' I'm able to capture only the numeric values not the strings in the data. Please someone tell me the procedure of importing such data.

Risposte (6)

Gareth Thomas
Gareth Thomas il 23 Mar 2014
Modificato: MathWorks Support Team il 7 Nov 2018
If you right click on the .csv file you can use the “Import Tool” which will help you through the import process. you will be able to choose what data types are imported.
If you click the “Import” button, you can also see the generated code or script. You will see the code it generates.
Another way is to use: “readtable”. See the “readtable” page examples on reading *.csv files.
I hope that this helps. Gareth

adam honse
adam honse il 9 Mar 2018
Modificato: Walter Roberson il 25 Set 2021
[~,~,data] = xlsread('myfile.csv')
works just fine for csvs.

Augustine Ekweariri
Augustine Ekweariri il 6 Mar 2017
You can use the csvread(csv_file.csv,row_offset,col_offset) command to read data from a csv file in this format;
a = csvread('csv_file.csv',1,0);
for more information, visit;

Stu Schwartz
Stu Schwartz il 6 Ago 2017
Input to csvread must be all numeric.
Mixed csv data can be input with table read and a format specification. See readtable online help and example: https://www.mathworks.com/help/matlab/import_export/import-mixed-text-and-numeric-data-from-a-text-file.html
  1 Commento
Walter Roberson
Walter Roberson il 6 Ago 2017
Historically it was true that input to csvread had to be all numeric. However, somewhere around R2015a, the range specification to csvread became able to skip over leading text rows and leading text columns (provided that the user asked to skip them); it is not able to skip over trailing text rows or trailing text columns however.
However, this ability to skip over leading rows does not help the user read the strings as they asked. It would, though, allow for the possibility of fopen() the file, fgetl() one line, fclose(), split the input line to get the column headers, then csvread() the file telling it to skip the first row.

Accedi per commentare.


Saeed Nematshahi
Saeed Nematshahi il 14 Dic 2020
use
a = csvread('csv_file.csv',1,0);
this only remove first column.
by changing 0 to 1 you can remove first column and first zero.

ehsan ghane
ehsan ghane il 25 Set 2021
for matlab R2020b, use readmatrix fnction.
  2 Commenti
Walter Roberson
Walter Roberson il 25 Set 2021
readmatrix() is not able to read text and numeric in the same file. The user had said "Using 'csvimport' I'm able to capture only the numeric values not the strings in the data." which I think means they wanted the text as well.

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by