variables to import in readtable for a csv file

조회 수: 1 (최근 30일)
Mihai Milea
Mihai Milea 2021년 9월 27일
댓글: Stephen23 2021년 9월 27일
Hi . I have the following maybe silly question: I have a csv file where the first row are some numerical values and second row some names. I attached a test file.
I used openFileName = strcat(pricedir,'/', 'test.csv');
opts=detectImportOptions(openFileName);
opts.VariableNamingRule = "preserve";
open=readtable(openFileName,opts);
While the first row is correct the second one is all NaN
  댓글 수: 2
Mathieu NOE
Mathieu NOE 2021년 9월 27일
hello
readtable works for column oriented files - not like your csv file which is row oriented
Mihai Milea
Mihai Milea 2021년 9월 27일
Thank you . So what do I use instead?

댓글을 달려면 로그인하십시오.

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 9월 27일
hello
as mentionned above , readtable is not appropriate for extracting row oriented files
you can do this (based on your example) :
filename = 'test.csv';
[data_num, data_str] = myreadfunction(filename)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [data_num, data_str]= myreadfunction(filename)
a = readlines(filename);
[m,~] = size(a);
data_num = str2num(char(a(1,:))); % first line
data_str = split(char(a(2,:)),','); % second line
end
  댓글 수: 2
Mihai Milea
Mihai Milea 2021년 9월 27일
Works perfect .Many thanks!
Stephen23
Stephen23 2021년 9월 27일
Avoiding NUM2STR (which hides evil EVAL inside):
tmp = readlines('test.csv');
num = str2double(split(tmp{1},','))
num = 3×1
NaN 23 35

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by