Updating surface colors when new data is assigned

I'm having issues with the colors not changing when the surface data is being updated. The following code is a minimal snippet.
h = surface(X,Y,Z);
%update Z
h.ZData = Z;
drawnow() %surface plot changes, but color does not
pause(0.1);
In this code, I have created a surface figure. When I alter Z, the surface plot actually changes, but the colors do not. The inserted photos below show this issue.
Before the update of Z:
help1.png
After an update of Z:
help2.png
For some reason, despite the values being altered, the color remains yellow, even though I would like the colors to be different since they are at different values. What am I missing? Does this issue not occur in other versions of Matlab?

 Accepted Answer

Adam Danz
Adam Danz on 5 Feb 2019
Edited: Adam Danz on 5 Feb 2019
Nathan,
The color data are stored in h.CData. When the plot is created it automatically assigns color based on the coordinate values but when you change the coordinates the color assigment is not updated.
Your best bet is probably just to redraw the surface with the new X,Y,Z coordinates. If that's not an option you'll nee to calcuate the new color data and update that along with the Z data.

3 Comments

Thanks for your response Adam.
So is there no way to ask Matlab to automatically reassign these colors, or is there a better way to perform an update to the figure so the colors are reassigned? I don't want to have to recreate a new figure window every time, given that the update to Z is within a loop and should be displayed at the end of the loop.
Edit: Just saw your new edited response, thank you.
You don't have to create a new figure. Just save the handle, delete it, and replace it.
For example,
figure
axh = axes;
hold(axh, 'on')
h = surface(axh, ...);
for i = 2:n
delete(h)
h = surface(axh, ...);
end

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!