variables to import in readtable for a csv file
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mihai Milea
am 27 Sep. 2021
Kommentiert: Stephen23
am 27 Sep. 2021
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 Kommentare
Mathieu NOE
am 27 Sep. 2021
hello
readtable works for column oriented files - not like your csv file which is row oriented
Akzeptierte Antwort
Mathieu NOE
am 27 Sep. 2021
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 Kommentare
Stephen23
am 27 Sep. 2021
Avoiding NUM2STR (which hides evil EVAL inside):
tmp = readlines('test.csv');
num = str2double(split(tmp{1},','))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!