Naming Cell Array as Rownames

17 vues (au cours des 30 derniers jours)
Frank Lehmann
Frank Lehmann le 1 Avr 2023
Hi All,
Ive run the code below with the output as listed from a Table via Excel
A =
7×5 table
Var1 Var2 Var3 Var4 Var5
____ ______ ______ _____ _____
185 1900 0.206 289.79 0.93 0.955
200 2000 0.187 312.98 0.927 0.959
220 2090 0.1876 334.89 0.956 0.956
250 2480 0.1904 392.5 0.924 0.959
280 2550 0.1877 440.54 0.923 0.958
315 2950 0.2163 497.16 0.923 0.955
355 3370 0.2158 557.93 0.924 0.958
rownames =
8×1 cell array
{'185'}
{'200'}
{'250'}
{'300'}
{'355'}
{'400'}
{'500'}
{'650'}
rownames2 =
'185'
1) My issue is how to designate the Row Names as the complete list from myvariable mK cell array as part of the table?
2) Then call out rownames {1} from the (table with Row names) and then get the complete row from the table?
Ive tried a few ideas to no avail, Im sure the answer is straight forward :)
Cheers
mK={'185','200','250','300','355','400','500','650'};
A=readtable('HV_MOTOR_MCCB_TABLES.xlsx','Sheet','6.6_MOTOR_TABLE','Range','I30:N36','VariableNamingRule','preserve','ReadRowNames',true)
rownames=mK'
rownames2=mK{1}
  1 commentaire
Vilém Frynta
Vilém Frynta le 1 Avr 2023
Modifié(e) : Vilém Frynta le 1 Avr 2023
If I understand correctly, you want to assign these numbers into the first column, to work as names for the rows.
You can create a new table (T_new) with the sizes same as the old table (T_old) but 1 extra column (for the row names). Then, you will insert these rownames into the first column, and the old table into the rest of the new table.
An example:
% Random table to work with
T = array2table(rand(8, 3))
T = 8×3 table
Var1 Var2 Var3 _________ ________ _______ 0.0087305 0.92326 0.55125 0.0050112 0.096651 0.28941 0.45911 0.16803 0.88101 0.313 0.81918 0.27493 0.8819 0.16557 0.95719 0.61809 0.021461 0.85675 0.7285 0.61692 0.41677 0.14007 0.73742 0.77508
% Your row names
mK={'185','200','250','300','355','400','500','650'};
% Create new table with extra column
[r c] = size(T);
T_new = array2table(zeros(r, c+1));
T_new.Var1 = mK(:);
T_new(:,2:end) = T
T_new = 8×4 table
Var1 Var2 Var3 Var4 _______ _________ ________ _______ {'185'} 0.0087305 0.92326 0.55125 {'200'} 0.0050112 0.096651 0.28941 {'250'} 0.45911 0.16803 0.88101 {'300'} 0.313 0.81918 0.27493 {'355'} 0.8819 0.16557 0.95719 {'400'} 0.61809 0.021461 0.85675 {'500'} 0.7285 0.61692 0.41677 {'650'} 0.14007 0.73742 0.77508
% Call out row '400'
idx = find(contains(T_new.Var1,'400'))
idx = 6
Hope I helped.

Connectez-vous pour commenter.

Réponses (1)

Peter Perkins
Peter Perkins le 5 Avr 2023
It's hard to tell what you are asking for. It mighht be this:
T = array2table(rand(5,2))
T = 5×2 table
Var1 Var2 _______ ________ 0.88692 0.14532 0.0409 0.65604 0.68256 0.059179 0.59393 0.25029 0.28182 0.818
T.Properties.RowNames = ["A";"B";"C";"D";"E"]
T = 5×2 table
Var1 Var2 _______ ________ A 0.88692 0.14532 B 0.0409 0.65604 C 0.68256 0.059179 D 0.59393 0.25029 E 0.28182 0.818
T("A",:)
ans = 1×2 table
Var1 Var2 _______ _______ A 0.88692 0.14532
T.Var1("A")
ans = 0.8869
T{"A",:}
ans = 1×2
0.8869 0.1453
But if your rownames are things like 185 and 200, you have a decision to make. Are these numbers, or names that just happen to be all digits.
T.Properties.RowNames = ["185" "200" "250" "300" "355"]
T = 5×2 table
Var1 Var2 _______ ________ 185 0.88692 0.14532 200 0.0409 0.65604 250 0.68256 0.059179 300 0.59393 0.25029 355 0.28182 0.818
T("185",:)
ans = 1×2 table
Var1 Var2 _______ _______ 185 0.88692 0.14532
and even
T(["185" "200"],:)
ans = 2×2 table
Var1 Var2 _______ _______ 185 0.88692 0.14532 200 0.0409 0.65604
But if you want to do anything numeric with those "names" you will need to add them as a variable and use logical subscripting:
T.Properties.RowNames = {};
T.ID = [185;200;250;300;355]
T = 5×3 table
Var1 Var2 ID _______ ________ ___ 0.88692 0.14532 185 0.0409 0.65604 200 0.68256 0.059179 250 0.59393 0.25029 300 0.28182 0.818 355
T(T.ID==185,:)
ans = 1×3 table
Var1 Var2 ID _______ _______ ___ 0.88692 0.14532 185
T(ismember([185 200],T.ID),:)
ans = 2×3 table
Var1 Var2 ID _______ _______ ___ 0.88692 0.14532 185 0.0409 0.65604 200

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by