Variable Names not being read into uitable using readmatrix

8 views (last 30 days)
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

Accepted Answer

Voss
Voss on 27 Jan 2025
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 Comments
Jason
Jason on 27 Jan 2025
Edited: Jason on 28 Jan 2025
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 ) :-)

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!