create a push button that updates a table in matlab

To create a push button in MATLAB that updates a table, you can use the MATLAB graphical user interface (GUI) functionalities. Here's an example code snippet to get you started:

main.m
% Create the GUI window
fig = uifigure('Name', 'Update Table Example', 'Position', [100 100 400 300]);

% Create a push button
button = uibutton(fig, 'Position', [150 150 100 30], 'Text', 'Update Table');

% Create a table
tableData = {'John', 25, 'USA'; 'Emily', 30, 'UK'; 'Michael', 28, 'Australia'};
columnNames = {'Name', 'Age', 'Country'};
table = uitable(fig, 'Position', [50 50 300 80], 'Data', tableData, 'ColumnName', columnNames);

% Define the button's callback function
button.ButtonPushedFcn = @(btn, event) updateTable();

% Define the table update function
function updateTable()
    % Retrieve the current data from the table
    currentData = table.Data;
    
    % Update the table data (e.g., change the age of the first person)
    currentData{1, 2} = currentData{1, 2} + 1;
    
    % Update the table with the modified data
    table.Data = currentData;
end
880 chars
26 lines

In this example, a GUI window is created using uifigure. A push button is added using uibutton, and a table is added using uitable. The button's ButtonPushedFcn is set to a callback function updateTable() which modifies the table's data by increasing the age of the first person by 1. Finally, the table is updated with the modified data.

Note: Make sure to adjust the positions and sizes of the GUI elements according to your preference. Run the code to see the GUI window with the push button and table. Clicking the push button will update the table, as specified in the updateTable() function.

You can customize the code to suit your specific needs, such as reading data from external sources or updating multiple columns of the table.

gistlibby LogSnag