How to add two sets of imported data?
古いコメントを表示
Hi
I am currently working on a program that requires me to add two sets of data that is imported in tables. However whenever I run the code, I get this error message:
Operator '+' is not supported for operands of type 'table'.
Error in MastersDraft_1 (line 3)
Moisture_content = plus(Lime_additioncombinedprdts,Lime_additioncombinedprdtsratio);
How do I fix it?
4 件のコメント
KSSV
2021 年 12 月 7 日
You cannot add tables like that. You want to join tables or add the respective columns?
Jan
2021 年 12 月 7 日
Adding tables is not meaningful in a mathematical sense. AS KSSV has mentioned already, joining the tables might be meant, or adding the contents of the table.
Ashley Bhugwandin
2021 年 12 月 8 日
Peter Perkins
2021 年 12 月 10 日
Please see my post below.
回答 (2 件)
Starting R2023a, tables now directly support arithmetic operations like plus, minus, sum, etc. So this kind of work becomes very easy. You can directly add two tables or numeric values to a table.
T1 = table([1;3],[2;4]);
T2 = table([1;2],[3;4]);
T = T1 + 10
T = T1 + T2
You can find more information about what things are now supported here: Direct Calculations on Tables and Timetables
Peter Perkins
2021 年 12 月 7 日
Ashley, there's a long example in the doc that talks about "how to perform calculations by using the numeric and categorical data that the table contains" at length:
Without more information in your question, the short answer is to do things like
t.X = t.X + 1
or
tplus1 = varfun(@(x)x+1,t)
or perhaps
t{:,["X" "Y" "Z"]) = t{:,["X" "Y" "Z"]) + 1
or
t.Variables = t.Variables + 1
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!