Variable Names not being read into uitable using readmatrix

Hi, I am trying to read a csv files into a uitable and am having problems reading the variable names in. The csv was saved using writetable and the first few lines are here:
Idx,pos,sep1,sep2,avgsep,deltaPix
1,101.1,1799.918,1868.078,1833.998,0.769999999999
2,101.2,1801.527,1868.695,1835.111,1.88299999999
3,101.3,1802.028,1869.09,1835.559,2.3309999999
4,101.4,1801.645,1869.739,1835.692,2.46399999999
5,101.5,1802.023,1869.453,1835.738,2.50999999999
This is my code to read the file :
table=app.UItable;
C=readtable(fullpath,'VariableNamingRule','preserve','FileType','text');
C=table2array(C); % Wasn't sure if I needed this
opts = detectImportOptions(fullpath)
opts.VariableNames
ReportMessage(app,'Opened Successfully')
table.Data=C;
Its not reading in the variable name
The opts.variablenames is showing the variable names are present
ans =
1×6 cell array
{'Idx'} {'pos'} {'sep1'} {'sep2'} {'avgsep'} {'deltaPix'}
and opts is:
opts =
DelimitedTextImportOptions with properties:
Format Properties:
Delimiter: {','}
Whitespace: '\b\t '
LineEnding: {'\n' '\r' '\r\n'}
CommentStyle: {}
ConsecutiveDelimitersRule: 'split'
LeadingDelimitersRule: 'keep'
TrailingDelimitersRule: 'ignore'
EmptyLineRule: 'skip'
Encoding: 'UTF-8'
Replacement Properties:
MissingRule: 'fill'
ImportErrorRule: 'fill'
ExtraColumnsRule: 'addvars'
Variable Import Properties: Set types by name using setvartype
VariableNames: {'Idx', 'pos', 'sep1' ... and 3 more}
VariableTypes: {'double', 'double', 'double' ... and 3 more}
SelectedVariableNames: {'Idx', 'pos', 'sep1' ... and 3 more}
VariableOptions: Show all 6 VariableOptions
Access VariableOptions sub-properties using setvaropts/getvaropts
VariableNamingRule: 'modify'
Any reason why the variable names aren't being populated into the uitable?
Thanks

 採用された回答

Voss
Voss 2025 年 1 月 27 日

0 投票

Using C = table2array(C) makes C a numeric array rather than a table array. Numeric arrays only contain numbers (no information about column or row names), so that's why the uitable's ColumnName property does not change when you set the uitable's Data property. You'd have to set the ColumnName property separately, similar to this:
C = readtable(fullpath,'VariableNamingRule','preserve','FileType','text');
C = table2array(C);
app.UItable.Data = C;
app.UItable.ColumnName = C.Properties.VariableNames;
However, if you omit using table2array and set the uitable's Data to table array (rather than numeric array) C, then the ColumnName is updated automatically:
C = readtable(fullpath,'VariableNamingRule','preserve','FileType','text');
app.UItable.Data = C;

5 件のコメント

Jason
Jason 2025 年 1 月 27 日
編集済み: Jason 2025 年 1 月 27 日
hmm, that still doesn't work for me
C.Properties.VariableNames
Dot indexing is not supported for variables of this type.
But this does work:
opts = detectImportOptions(fullpath);
opts.VariableNames
table.ColumnName = opts.VariableNames;
So if I was to leave the uitable data as "table" class rather than "double" by using table2array, is there any disadvantage? I need to perform calculations on each the data
Voss
Voss 2025 年 1 月 27 日
"So if I was to leave the uitable data as "table" class rather than "double" by using table2array, is there any disadvantage?"
Not really, as long as you know how to deal with tables: https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html
"that still doesn't work for me"
What is the value of C immediately after
C = readtable(fullpath,'VariableNamingRule','preserve','FileType','text');
?
Jason
Jason 2025 年 1 月 27 日
C =
200×6 table
Idx pos sep1 sep2 avgsep deltaPix
___ _____ ______ ______ ______ ________
1 101.1 1799.9 1868.1 1834 0.77
2 101.2 1801.5 1868.7 1835.1 1.883
3 101.3 1802 1869.1 1835.6 2.331
4 101.4 1801.6 1869.7 1835.7 2.464
Jason
Jason 2025 年 1 月 27 日
編集済み: Jason 2025 年 1 月 28 日
ahh, I think its because I didnt put it above the table2array conversion
C=readtable(fullpath,'VariableNamingRule', 'preserve') % C=readtable(fullpath,'VariableNamingRule', 'preserve','FileType','text');
C.Properties.VariableNames
C=table2array(C);
It now works, thanks
(seems I may need to convert all my uitable data into tables as there's some nice tools / function to use with table (i.e. metadata, such as description ) :-)
Voss
Voss 2025 年 1 月 27 日
Glad it's working!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

製品

リリース

R2023b

質問済み:

2025 年 1 月 27 日

編集済み:

2025 年 1 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by