Naming Cell Array as Rownames
19 次查看(过去 30 天)
显示 更早的评论
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 个评论
Vilém Frynta
2023-4-1
编辑:Vilém Frynta
2023-4-1
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))
% 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
% Call out row '400'
idx = find(contains(T_new.Var1,'400'))
Hope I helped.
回答(1 个)
Peter Perkins
2023-4-5
It's hard to tell what you are asking for. It mighht be this:
T = array2table(rand(5,2))
T.Properties.RowNames = ["A";"B";"C";"D";"E"]
T("A",:)
T.Var1("A")
T{"A",:}
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("185",:)
and even
T(["185" "200"],:)
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(T.ID==185,:)
T(ismember([185 200],T.ID),:)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!