Using data from table in calculations
21 views (last 30 days)
Show older comments
I have recently started using MATLAB and have a basic understanding of it. I have a table with 99000 rows (these rows are values at particular times) in it and want to perform the same calculation on each row giving an output, then moving to the next row, perform the calculation and so on.
I can perform the calculation by manually entering the numbers but want my script to be able to get the values itself. I have tried calling the input columns like:
X=(TableName.ColumnName1);
Y=(TableName.ColumnName2);
Z=(TableName.ColumnName3);
and they do give out an array. But then I get an error when it tries to put X, Y & Z through the calculation. Is there a certain loop I need to do to go through this in iterations?
(p.s, I have searched online and on the Using Tables section of MATLAB help but to no avail.)
Any help is appreciated
Thanks!
2 Comments
Mario Malic
on 2 Feb 2021
Hello,
Here's an example
load patients.mat
t = table(Height,Weight)
t.Height
This will return a vector of numeric values, just like your X variable. How are you using your X in the code? Do you have another vector that you want it to multiply with, or you have a scalar that you're multiplying it with?
Answers (1)
Adam Danz
on 2 Feb 2021
Have you tried a simple loop like this?
% Function returns a scalar value and stores it in a vector.
% Function contains 3 inputs.
out = zeros(1, height(TableName)); % preallocate; assuming a vector
for i = 1:height(TableName)
out(i) = fun(TableName.Col1(i), TableName.Col2(i), TableName.Col3(i));
end
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!