Reloading a figure when a push button is pushed
4 views (last 30 days)
Show older comments
Hello
I'm trying to make a dataset by a 5*5 push buttons, i have made a push button named ok, i want to save the data after pushing that ok button and then reload figure,make and save the new data,but i don't know how to refresh(reload) the figure as it was at first to get the new data,any body could help me?
Here is the gui of what i have done...

0 Comments
Answers (1)
Satyam
on 26 Mar 2025
To address the need for saving button states and resetting them to their original settings, consider maintaining an array to track the states of each button. Here's a step-by-step approach:
Use an array "buttonStates" to keep track of the state of each button. As a user interacts with the buttons, update the corresponding entry in this array to reflect the current state.
% Function to toggle button state
function toggleButtonState(row, col, btn)
buttonStates(row, col) = ~buttonStates(row, col);
% Update button color to reflect its state
if buttonStates(row, col)
btn.BackgroundColor = 'red';
else
btn.BackgroundColor = defaultColor; % Reset to default color if clicked again
end
end
When the "Submit" button is pressed, save the "buttonStates" array to a .mat file.
After saving, reset all values in the "buttonStates" array to zero. Simultaneously, revert the "BackgroundColor" of all buttons to their default color, ensuring the grid returns to its initial state. Refer to the following documentation, which explains how to set the background color of a button. https://www.mathworks.com/help/matlab/ref/uibutton.html#namevaluepairs
% Function to save data and reset grid
function saveAndReset()
% Save current button states to a .mat file
save('buttonData.mat', 'buttonStates');
% Reset button states
buttonStates = zeros(5, 5);
% Update button appearances to reflect reset state
for i = 1:5
for j = 1:5
buttons(i, j).BackgroundColor = defaultColor; % Reset to default color
end
end
end
A sample code to illustrate the above approach has been attached.
I hope this helps!
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks 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!