Using a column of cells as variable names in a table

4 views (last 30 days)
Hi!
I have a table that contains 2 columns each having 28 rows. The first column contains cells of strings, and the second one contains numbers in cell format. I want to use the individual strings from column 1 as the varible names for a table and then assign the numbers from 2nd column to those variables. How do I do that? The file is attached.
Any help will be appreciated.

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 18 Feb 2023
Here is how to get this assignment done:
T = readtable('Properties.xlsx');
T2 = table(T.Var2, 'RowNames',T.Var1)
T2 = 28×1 table
Var1 _______ a1 48 b1 96 t1 0.25 E_steel 3e+07 v_steel 0.33 P1 1 P2 10000 a2 1 b2 1.5 t2 0.01 E_al 6.9e+10 v_al 0.3 Sinusoidal 1 Uniform 2 Hydrostatic 3 Patch 4
T2 = rows2vars(T2)
T2 = 1×29 table
OriginalVariableNames a1 b1 t1 E_steel v_steel P1 P2 a2 b2 t2 E_al v_al Sinusoidal Uniform Hydrostatic Patch m_max n_nax x_sigmax y_sigmax x_tauxy y_tauxy x_tauxz y_tauxz x_tauyz y_tauyz x_sigmaz z_sigmaz _____________________ __ __ ____ _______ _______ __ _____ __ ___ ____ _______ ____ __________ _______ ___________ _____ _____ _____ ________ ________ _______ _______ _______ _______ _______ _______ ________ ________ {'Var1'} 48 96 0.25 3e+07 0.33 1 10000 1 1.5 0.01 6.9e+10 0.3 1 2 3 4 23 23 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
T2.a1
ans = 48
T2.b1
ans = 96
T2.t1
ans = 0.2500
T2.E_steel
ans = 30000000
...

Seth Furman
Seth Furman on 14 Mar 2023
Edited: Seth Furman on 14 Mar 2023
See unstack.
t1 = readtable("Properties.xlsx")
t1 = 28×2 table
Var1 Var2 _______________ _______ {'a1' } 48 {'b1' } 96 {'t1' } 0.25 {'E_steel' } 3e+07 {'v_steel' } 0.33 {'P1' } 1 {'P2' } 10000 {'a2' } 1 {'b2' } 1.5 {'t2' } 0.01 {'E_al' } 6.9e+10 {'v_al' } 0.3 {'Sinusoidal' } 1 {'Uniform' } 2 {'Hydrostatic'} 3 {'Patch' } 4
t2 = unstack(t1,"Var2","Var1")
t2 = 1×28 table
E_al E_steel Hydrostatic P1 P2 Patch Sinusoidal Uniform a1 a2 b1 b2 m_max n_nax t1 t2 v_al v_steel x_sigmax x_sigmaz x_tauxy x_tauxz x_tauyz y_sigmax y_tauxy y_tauxz y_tauyz z_sigmaz _______ _______ ___________ __ _____ _____ __________ _______ __ __ __ ___ _____ _____ ____ ____ ____ _______ ________ ________ _______ _______ _______ ________ _______ _______ _______ ________ 6.9e+10 3e+07 3 1 10000 4 1 2 48 1 96 1.5 23 23 0.25 0.01 0.3 0.33 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!