My variable names for a table aren't the same length even though they are both 20 long and won't change

2 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

Voss
Voss le 3 Juin 2022
Modifié(e) : Voss le 3 Juin 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
Names = 1×20 cell array
{'CH1'} {'CH2'} {'CH3'} {'CH4'} {'CH5'} {'CH6'} {'CH7'} {'CH8'} {'CH9'} {'CH10'} {'CH11'} {'CH12'} {'CH13'} {'CH14'} {'CH15'} {'CH16'} {'CH17'} {'CH18'} {'CH19'} {'CH20'}
% 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)
cell_data = 1×20 cell array
{5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double} {5×1 double}
% and use that as arguments in table, to get a table with multiple
% variables (rather than a table with a single variable, each row
t = 5×20 table
CH1 CH2 CH3 CH4 CH5 CH6 CH7 CH8 CH9 CH10 CH11 CH12 CH13 CH14 CH15 CH16 CH17 CH18 CH19 CH20 _______ _______ _______ _______ ________ _______ ________ _______ _______ ________ ________ _______ ________ _______ ________ _______ _______ _______ ________ ________ 0.83495 0.57324 0.15406 0.71764 0.15624 0.48722 0.41819 0.70706 0.47438 0.04234 0.6775 0.77621 0.093156 0.77831 0.22619 0.49245 0.95948 0.63162 0.034427 0.070848 0.35707 0.35558 0.94107 0.42838 0.018153 0.95162 0.99955 0.11608 0.29396 0.66961 0.80899 0.50561 0.77366 0.55691 0.080486 0.8886 0.40172 0.16117 0.83815 0.55945 0.84943 0.34997 0.17635 0.71218 0.62415 0.76967 0.58989 0.6685 0.66363 0.25907 0.7817 0.43712 0.1938 0.81385 0.10315 0.43355 0.6711 0.68076 0.11415 0.24839 0.2884 0.76242 0.51344 0.73116 0.18381 0.89611 0.087377 0.37085 0.32857 0.2294 0.050061 0.99957 0.38818 0.54801 0.41 0.4613 0.64436 0.68862 0.81377 0.26697 0.77982 0.29926 0.12153 0.87523 0.95481 0.16382 0.77678 0.99808 0.73635 0.068061 0.76021 0.60053 0.02392 0.57137 0.8207 0.69833 0.69633 0.70762 0.30656 0.89504
% 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)

Plus de réponses (0)

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by