How do I range through a set of columns in a table to generate data and store data in new columns?
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I've got a 33x12 table "T", where I'd like to generate 8 new columns of data ("Result 1 - 8") by doing a simple calculation of going through values in Column 1-8 one at a time and multiplying each of them with data in Column 12 ("FixedConcentration") and muliplying that by 100. I can do this calculation for one column at a time by doing this...
T.Result1 = (T.APConcentration_mM__1)./(T.FixedConcentration).*100
...but would like this to be done automatically. I use readtable to get the excel file into MATLAB but I'm stuck on how to create a loop that accesses the data column 1-8. Apologies if this is a simple question, but I have limited programming experience and I'd appreciate any help.
Thank you.
0 个评论
采纳的回答
Star Strider
2021-3-9
Try this:
T1 = readtable('matlab table.xlsx', 'VariableNamingRule','preserve');
Result = array2table(T1{:,1:8}.*T1.FixedConcentration*100, 'VariableNames',compose('Result%d',1:8));
T1 = [T1,Result];
The first five rows of the new table are then:
Value1 Value2 Value3 Value4 Value5 Value6 Value7 Value8 ID 1 ID 2 Quantity FixedConcentration Result1 Result2 Result3 Result4 Result5 Result6 Result7 Result8
__________ __________ __________ __________ __________ __________ __________ __________ ______ ______ ______________________ __________________ _______ _______ _______ _________ _______ _______ ________ ________
-0.0034822 -0.0087225 -0.0096823 -0.010581 -0.0075441 -0.0063167 -0.0050771 -0.0033302 {'A1'} {'1' } {'2.896930253098927' } 1.8244 -0.6353 -1.5914 -1.7665 -1.9304 -1.3764 -1.1524 -0.92628 -0.60758
0.05166 0.062327 0.053914 0.059795 0.073784 0.050638 0.052084 0.041851 {'A2'} {'9' } {'1.8881568931671828'} 4.2058 21.727 26.214 22.675 25.149 31.032 21.297 21.906 17.602
-0.011543 -0.0090813 -0.0099802 -0.010053 -0.01124 -0.010381 -0.0097284 -0.0092605 {'A3'} {'17'} {'2.8327923105329047'} 3.7978 -4.3837 -3.4489 -3.7903 -3.8178 -4.2688 -3.9424 -3.6947 -3.517
0.016241 0.016529 0.023821 -0.0031858 0.026756 0.046819 0.028462 0.057366 {'A4'} {'25'} {'1.823551230561143' } 0.28849 0.46853 0.47685 0.68719 -0.091905 0.77189 1.3507 0.82109 1.6549
0.021792 0.026289 0.041477 0.037204 0.020308 0.021495 0.030704 0.0308 {'A5'} {'33'} {'2.108249264604965' } 4.9974 10.89 13.138 20.728 18.592 10.149 10.742 15.344 15.392
Note that your question text mentioned ‘multiply’, however the example as a division, so this may be what you actually want:
Result = array2table(T1{:,1:8}./T1.FixedConcentration*100, 'VariableNames',compose('Result%d',1:8));
The rest of the code is unchanged.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!