Printing a MATLAB table on the console which contains both characters and ints

63 vues (au cours des 30 derniers jours)
I am trying to print a table on the console (using the table function) that displays both characters and numerical values in the same table. My current code is shown below. How do I make it such that I can remove the { and ' characters from all of the parameters in the "Name of Value" column? Any help would be greatly appreciated. Thank you
col = {'Name of value', 'Value'};
titles = {'Mileage (km)', 'Tire Pressure (bars)', 'License Points'}';
vals = [20000, 2.4, 3]';
configTable = cell2table([titles, num2cell(vals)], 'VariableNames', col);
disp(configTable)
Name of value Value ________________________ _____ {'Mileage (km)' } 20000 {'Tire Pressure (bars)'} 2.4 {'License Points' } 3
  1 commentaire
Stephen23
Stephen23 le 29 Août 2021
Use categorical or char:
X = ["Mileage (km)"; "Tire Pressure (bars)"; "License Points"];
Y = [20000; 2.4; 3];
T = table(char(X), Y, 'VariableNames', ["Name of value", "Value"])
T = 3×2 table
Name of value Value ____________________ _____ Mileage (km) 20000 Tire Pressure (bars) 2.4 License Points 3

Connectez-vous pour commenter.

Réponse acceptée

Ive J
Ive J le 28 Août 2021
One simple way is to convert them to categorical, but if you're doing something with that variable, extra care should be taken.
col = {'Name of value', 'Value'};
titles = {'Mileage (km)', 'Tire Pressure (bars)', 'License Points'}';
vals = [20000, 2.4, 3]';
configTable = cell2table([titles, num2cell(vals)], 'VariableNames', col);
configTable.(1) = categorical(configTable.(1));
disp(configTable)
Name of value Value ____________________ _____ Mileage (km) 20000 Tire Pressure (bars) 2.4 License Points 3

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by