Variable Names not being read into uitable using readmatrix

조회 수: 4 (최근 30일)
Jason
Jason 2025년 1월 27일
편집: Jason 2025년 1월 28일
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일
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월 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개)

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by