Need help with Matlab GUI – how to refresh/ redraw?

In summary, the conversation discussed the issue of the Axes object not refreshing immediately in a GUI. The suggested solutions were to use the drawnow function or the pause function after clearing the plot to force MATLAB to update the graphics display before proceeding to the next step.
  • #1
NikTuz
1
0
Hello:

Could you possibly help me. I am writing a fairly simple GUI. The callback for “Start” button does the following:

1) Clears the current plot displayed in an Axis object
2) Calculates data for a new plot
3) Plots the new plot in Axes object from 1)

I need 1) to take place instantly, i.e. before 2) because 2) takes lots of time and I don’t want the old plot to be displayed while 2) is running. That’s the sequence I put it in the callback, but in practice the Axes objects get cleared only after 2) is done. Is there any way to refresh the Axes object before the callback is completed?

Thanks in advance,
Nik
 
Physics news on Phys.org
  • #2
ita

Hello Nikita,

Thank you for reaching out. I understand your frustration with the current sequence of events in your GUI. It is possible to refresh the Axes object before the callback is completed, but it requires some additional coding.

One approach you can take is to use the drawnow function in MATLAB. This function forces MATLAB to update the graphics display immediately, instead of waiting for the callback to finish. You can use this function after calling the "cla" command to clear the current plot. This way, the Axes object will be cleared before the callback continues to the next step.

Another option is to use the "pause" function after clearing the plot. This will give MATLAB a brief moment to update the graphics display before continuing with the next step.

I hope this helps. Let me know if you have any further questions or need clarification on anything. Good luck with your GUI!


 
  • #3
ita

Hello Nikita,

Thank you for reaching out for help with your Matlab GUI. It sounds like you are having trouble with the sequence of events in your callback function. In order to have the Axes object cleared before the new plot is calculated and displayed, you can use the 'cla' command to clear the current axes before calling your calculation function. This will ensure that the old plot is removed before the new one is displayed. Additionally, you can use the 'drawnow' command after the 'cla' command to force the GUI to update and display the cleared axes before moving on to the next step. Your code may look something like this:

% Callback function for Start button
function startButton_Callback(hObject, eventdata, handles)
% Clear current plot from the axes
cla(handles.axes1);
% Update the GUI to display the cleared axes
drawnow;
% Calculate data for new plot
newPlotData = calculateNewPlotData();
% Plot the new data in the cleared axes
plot(handles.axes1, newPlotData);
% Update the GUI to display the new plot
drawnow;
end

I hope this helps with your issue. Let me know if you have any further questions or if this solution does not work for you.

Best,
 

FAQ: Need help with Matlab GUI – how to refresh/ redraw?

How do I refresh or redraw my Matlab GUI?

The easiest way to refresh or redraw your Matlab GUI is to use the drawnow function. This will force the GUI to update and display any changes made to the interface.

Why is my Matlab GUI not refreshing or redrawing properly?

There could be a few reasons for this. First, make sure that you are using the drawnow function in the correct place, such as after making changes to the GUI. Also, check for any errors in your code that may be preventing the GUI from updating properly.

Can I set a specific refresh rate for my Matlab GUI?

Yes, you can use the pause function to set a specific refresh rate for your GUI. For example, pause(0.5) will refresh the GUI every 0.5 seconds.

Is there a way to refresh only a specific part of my Matlab GUI?

Yes, you can use the uicontrol('Style','text') function to create a text box and then use the set function to update the text in the box. This will refresh only the specified part of the GUI.

Can I use a button to trigger a refresh on my Matlab GUI?

Yes, you can use the Callback property of a button to call the drawnow function when the button is clicked. This will allow you to refresh the GUI at any time by clicking the button.

Similar threads

Replies
6
Views
1K
Replies
5
Views
2K
Replies
6
Views
4K
Replies
4
Views
3K
Replies
1
Views
2K
Replies
10
Views
2K
Replies
2
Views
3K
Back
Top