Adding a Calculation as a field in an existing table.

조회 수: 8 (최근 30일)
Sclay748
Sclay748 2020년 8월 19일
댓글: Walter Roberson 2020년 8월 19일
Hello,
I have a 1x100 structure called 'a'. (See table). It is located in a .mat file. It has 100 rows, but the first 7 are visable in the screenshot.
In my script, I have an equation that calculates dynamic_k1 and dynamic_k2 by multiplying 12 to each of the 100 values of capacitance. I am able to print dynamic_k1 and dynamic_k2 and the math is correct, but I am wondering how I can add those 2 new calculations to the table as 2 new columns with 100 data points each.
Thank you.

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 19일
Assuming that a is a struct array:
dynamic_k1 = 12 * vertcat(a.capacitance1);
dynamic_k2 = 12 * vertcat(a.capacitance2);
YourTable.dynamic_k1 = dynamic_k1;
YourTable.dynamic_k2 = dynamic_k2;
However, if a is your table then your syntax is wrong, and you should just do
a.dynanic_k1 = 12 * a.capacitance1;
a.dynanic_k2 = 12 * a.capacitance2;
  댓글 수: 3
Sclay748
Sclay748 2020년 8월 19일
편집: Sclay748 2020년 8월 19일
Hi Walter, sorry I was not able to try this until today. Power went out, (rolling california blackouts).
I tried this and got an error "scalar strucute required for this assignment. I tried both methods, but the table should be a structure, it says it at the top when I open it. 1x100struct. It is also named 'a'. This is what I did below.
dynamic_k1 = 12 * vertcat(a.capacitance1);
dynamic_k2 = 12 * vertcat(a.capacitance2);
a.dynamic_k1 = dynamic_k1;
a.dynamic_k2 = dynamic_k2;
Walter Roberson
Walter Roberson 2020년 8월 19일
If you are trying to assign a new field dynamic_k1 and dynamic_k2 into a structure array instead of into a table() object, then
dynamic_k1 = arrayfun(@(C.capacitance1) 12*C, a, 'uniform', 0);
dynamic_k2 = arrayfun(@(C.capacitance2) 12*C, a, 'uniform', 0);
[a.dynamic_k1] = dynamic_k1{:};
[a.dynamic_k2] = dynamic_k2{:};
Or let me see... maybe
a = arrayfun(@(S) setfield(S.dynamic_k1, 12*S.capacitance1), a);
a = arrayfun(@(S) setfield(S.dynamic_k2, 12*S.capacitance2), a);
But a loop might be more efficient:
for K = 1 : numel(a)
a(K).dynamic_k1 = 12 * a(K).capacitance1;
a(K).dynamic_k2 = 12 * a(K).capacitance2;
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by