The detectImportOptions not able to recognize and use 2nd row of a file as column name
Afficher commentaires plus anciens
filename = 'data.csv';
opts = detectImportOptions(filename);
% Adjust options to use the second row as headers
opts.DataLines = [3 Inf]; % Start reading data from the third row
opts.VariableNamesLine = 2; % Use the second row for column names
% Read the table
dataTable = readtable(filename, opts);
The above code is not able to return a data table with the columns names same as the names mention in second row of the file, Instead it renames the columns in datatable as Var1, Var2, Var3 and Var4 instead of second, volt, volt and Ampere.
Réponse acceptée
Plus de réponses (1)
Meg Noah
le 6 Août 2025
filename = 'data.csv';
dataTable = readtable(filename,'NumHeaderLines',1)
dataTable =
1×4 table
second Volt Volt_1 Ampere
______ ____ ______ ______
1 2 4 45
7 commentaires
Deep Shah
le 7 Août 2025
Yes, sometime between R2019a and R2021b 'HeaderLines' became 'NumHeaderLines' officially.
Either is recognized for backwards compatibility.
For historical context, textread is the predecessor to the (relatively) recently introduced family of readtable and friends; in it the named parameter was 'headerlines', the change to camel-case came with the introduction of the new functions and then the renaming to begin with 'Num' followed.
(I'm so old and such a longtime user, I frequently forget and revert to the old style. <vbg>)
Deep Shah
le 7 Août 2025
Meg Noah
le 7 Août 2025
Yes, it used to be 'HeaderLines' and now it is 'NumHeaderLines'. But you weren't specific about which version of MATLAB you were using, so I really did answer the question correctly first.
Walter Roberson
le 7 Août 2025
The question is tagged with R2019a. Look under "Release" in the upper right hand side of the page.
Meg Noah
le 7 Août 2025
@Walter Roberson OK - now I know where to look! On my browser, it is way down and doesn't display unless I scroll down to it. Too many 'See Alsos...' and 'Categories...'.
"...it is way down and doesn't display unless I scroll down to it. Too many 'See Alsos...' and 'Categories...'. "
Agreed. There's way too much wasted white space even in "compact" view.
At a minimum, the Tags and Release ought to be at the top with the Q?; "See Also"(*) and the advertising content can follow.
(*) If it were more smarter, it might make at least a little sense, but it's so often so far off-base as to be worse than none. Just looked at the top link in the other thread about Desktop Layout with R2025a -- the thread is dated 2011.
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
