How can I import data from .csv file with numeric values and texts (with column headers) into MATLAB Workspace?
조회 수: 221 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (6개)
Gareth Thomas
2014년 3월 23일
편집: MathWorks Support Team
2018년 11월 7일
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
댓글 수: 0
adam honse
2018년 3월 9일
편집: Walter Roberson
2021년 9월 25일
[~,~,data] = xlsread('myfile.csv')
works just fine for csvs.
댓글 수: 0
Augustine Ekweariri
2017년 3월 6일
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;
댓글 수: 0
Stu Schwartz
2017년 8월 6일
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
Walter Roberson
2017년 8월 6일
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.
Saeed Nematshahi
2020년 12월 14일
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.
댓글 수: 1
Walter Roberson
2020년 12월 15일
Someone already recommended that three years ago; https://www.mathworks.com/matlabcentral/answers/122717-how-can-i-import-data-from-csv-file-with-numeric-values-and-texts-with-column-headers-into-matlab#answer_257459
ehsan ghane
2021년 9월 25일
for matlab R2020b, use readmatrix fnction.
댓글 수: 2
Walter Roberson
2021년 9월 25일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!