My variable names for a table aren't the same length even though they are both 20 long and won't change
3 views (last 30 days)
Show older comments
How can I reset the variable names and change them to new ones? The variable names are "Var1_1, Var1_2..." are they only one variable? There are 20 columns. I'm using the App Designer and exporting them the excel using writetable.
global LineNumber
Names={'CH1','CH2','CH3','CH4','CH5','CH6','CH7','CH8','CH9','CH10','CH11','CH12','CH13','CH14','CH15','CH16','CH17','CH18','CH19','CH20'};
InputFile=('Temperature Table Line %s.xlsx');
filename=sprintf(InputFile,LineNumber);
t=table(app.UITable3.Data);
t.Properties.VariableNames=Names; % My error is here
writetable(t,filename,'WriteVariableNames',true)
%% The VariableNames property must contain one name for each variable in the table.
error ^
There are 20 CHs and 20 columns in UITable3, why isn't there one name for each variable?
0 Comments
Accepted Answer
Voss
on 3 Jun 2022
Edited: Voss
on 3 Jun 2022
I don't know what class app.UITable3.Data is, but in another question (here), you say that calling writematrix(app.UITable.Data) works but only includes the values.
That indicates that app.UITable.Data is a matrix, and if so, you can convert it to a cell array and use that cell array as a comma-separated list of arguments for the table function, in order to build a table with 20 variables:
% Names = {'CH1','CH2','CH3','CH4','CH5','CH6','CH7','CH8','CH9','CH10','CH11','CH12','CH13','CH14','CH15','CH16','CH17','CH18','CH19','CH20'};
Names = sprintfc('CH%d',1:20) % easier
% I'm using a made up matrix, since I don't have app.UITable3
data = rand(5,20);
% You would do this instead:
% data = app.UITable3.Data;
% Then convert the matrix data to an appropriate cell array:
cell_data = num2cell(data,1)
% and use that as arguments in table, to get a table with multiple
% variables (rather than a table with a single variable, each row
% of which has multiple elements):
t = table(cell_data{:},'VariableNames',Names) % and you can include the variable names here
Then use writetable with the table t as you already have it (or without 'WriteVariableNames',true, since that is the default):
writetable(t,filename)
0 Comments
More Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!