Is it possible to decide which line is variable names in readtable?

조회 수: 61 (최근 30일)
I want to use readtable to read data from an excel file. As I understand, in readtable, the first line is automatically the variable names. However, in my excel file, not just the first line, but the starting 3 lines contain characters and I want to know whether it is possible to use readtable to have an output where the vaiable names are the words that appear in the 3rd (or any desired) line.
A part of the excel file looks like this:
and I want the output to be like this:
ML125 MLCorr ML100 Ds
--------------------------------
32.2 32.2 48.44 15.2
...

채택된 답변

Walter Roberson
Walter Roberson 2022년 6월 24일
Yes, you can do that. For example,
variable_row = 3;
datatable = readtable(filename, 'VariableNamesRange', variable_row);
In some cases you want to specify a location to read the data from. You can use 'Range' in some cases, but if you have complicated headers then it might be better to use the 'DataRange' option.
Note: 'VariableNamesRange' and 'DataRange' may be specific to spreadsheets; the option names might be different for text files.
  댓글 수: 2
Saeid
Saeid 2022년 6월 24일
Thanks Walter. When I try this, and although the said rows have different names, the output ends up looking like:
Var1 Var2 Var3
(or some strange number) and so on. I als tried setting 'VariableNamingRule' to 'preserve' but still turns out some strange variable name that is not what appears in the excel table
Walter Roberson
Walter Roberson 2022년 6월 24일
Can you attach a sample xlsx file for us to test with?

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

추가 답변 (1개)

Jeremy Hughes
Jeremy Hughes 2022년 6월 24일
You can try:
readtable(filename,"NumHeaderLines",2)
or
opts = detectImportOptions(filename)
opts.VariableNamesRange = "A3"
T = readtable(filename,opts)

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by