can a timetable column reference a function handle automatically and dynamically add current value based on a set of computations?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Reviving this question.
Adding data: can always be done through adding a new column by referencing a function, such as:
timeTable.Data = functionOf(timeTable.ExistingVar);
But this is static. What if we could do this automatically when an event happens (add value/edit value) ...
Something along the lines of a column that automatically computes best/worst performers (max/min/etc) on all the other columns & does a set of operations based on its previous values ... say something like this:
newValue = previousValue + max(values in columns 1:10) on the last line.
This would also be similar to an excel colum defined with a (simple) formula based on another column(s) value(s). Can this be achieved through any Matlab construct / object?
thx
0 Commenti
Risposte (1)
Abhinaya Kennedy
il 3 Gen 2024
Hi Andy,
As I understand, you would like to achieve dynamic updates to a table or array. One way to achieve this is using event listeners and callback functions. MATLAB does not have a built-in feature that exactly replicates Excel's cell formula functionality, but you can simulate it by creating a function that updates your table when certain events occur.
To implement this, you can use MATLAB's event handling capabilities with its property listener functionality. Here's a conceptual example of how you could set up a MATLAB class that updates a table's column automatically when data in other columns change.
First, you would define a MATLAB class that contains the table and the methods to update it:
classdef DynamicTable < handle
properties
Data table
end
methods
function obj = DynamicTable(initialData)
obj.Data = initialData;
% Set up listeners for each variable in the table
for varName = initialData.Properties.VariableNames
addlistener(obj.Data, varName{1}, 'PostSet', @(src, event)obj.updateColumn());
end
end
function updateColumn(obj)
% Define the operation for the new value
% Assuming the last line is the one that changes
lastRow = height(obj.Data);
previousValue = obj.Data{lastRow, 'ComputedColumn'};
maxValue = max(obj.Data{lastRow, 1:10}, [], 2); % max across columns 1 to 10
newValue = previousValue + maxValue;
obj.Data{lastRow, 'ComputedColumn'} = newValue; % Update the computed column
end
end
end
To use this class, first initialize your table with the initial data, and then create an instance of `DynamicTable`:
% Example initial data
initialData = array2table(rand(10), 'VariableNames', {'Var1', 'Var2', 'Var3', 'Var4', 'Var5', 'Var6', 'Var7', 'Var8', 'Var9', 'Var10'});
initialData.ComputedColumn = zeros(10, 1); % Initialize the computed column
% Create an instance of the DynamicTable class
dynamicTable = DynamicTable(initialData);
Now, whenever you change the data in the first 10 columns of `dynamicTable.Data`, the `updateColumn` method will be called automatically to update the 'ComputedColumn'.
Please note that this example assumes that the last row is the one that is being updated. You would need to adjust the logic in the `updateColumn` method if changes can occur in rows other than the last one, or if multiple rows can be updated at once.
Hope this helps.
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!