Trisurf : How to interactively change the plot opacity?
9 次查看(过去 30 天)
显示 更早的评论
I'm making real progress with my humble 3D image / Device gamut plotting project. Here's what I'm getting now :
As you can see, this figure is combining a scatter3 and a trisurf plot -- amazing, And in a relatively small amount of code. I am really impressed. (To achieve the same result uing a traditional programming language and an openGL library would have taken me months!)
Question...
As you can see, there is a series of "colored points" that come from the scatter3 plot (those represent the color pixels in my image). And there is a device gamut that is semi-tranluscent. In this instance, I changed the opacity factor down from 1.0 down to 0.25, in the trisurf call (FaceAlpha), in order to better see the points inside the "volume / gamut" :
h = trisurf(k,a,b,L,'FaceColor','interp','FaceVertexCData',rgb,'EdgeColor','none', 'FaceAlpha', 0.25)
It works like a charm!
But, what if I would want to vary interactively the opacity of the plot, without having to stop the script, change the faceAlpha value and run it again?
I guess this is throwing me right into GUI programming, right? Where the entire figure would display inside some kind of "Figure control" and the user would interact with the figure through some other kind of control?
I've been avoiding to get into creating a user interface for my project ...
0 个评论
回答(2 个)
Jan
2021-12-19
GUIs are not hard to control.
function testGUI
% Some test data:
[x,y] = meshgrid(1:15, 1:15);
tri = delaunay(x,y);
z = peaks(15);
TriH = trisurf(tri,x,y,z);
uicontrol('Style', 'Slider', 'Value', 1, 'Callback', {@sliderCB, TriH});
end
function sliderCB(SliderH, EventData, TriH)
alpha = SliderH.Value; % Get current value of slider
set(TriH, 'FaceAlpha', alpha); % Set opacity
end
Here a simple slider influences the transparency. Another option would be using the + and - keys and the WindowKeyPressFcn of the figure.
Using modern uifigures and the AppDesigner offers much more power, but creating the GUIs programmatically is sufficient for such small interactions also.
3 个评论
Jan
2021-12-20
Store the file in an M-file insider your path. Then you can start it using the green triangle in the editor or by calling "testGUI" from the command line or another function.
You can use the properties of the slider to modify it. See: doc uicontrol
uicontrol('Style', 'Slider', 'Value', 1, 'Callback', {@sliderCB, TriH}, ...
'Units', 'normalized', 'Position', [0.1, 0.9, 0.8. 0.05]);
"Normalized" means, that the position is set relative to the figure. The position is defined as [x position, y position, width, height].
You can create a callback which is triggered during moving the slider, but this is not trivial:
function testGUI
% Some test data:
[x,y] = meshgrid(1:15, 1:15);
tri = delaunay(x,y);
z = peaks(15);
TriH = trisurf(tri,x,y,z);
FigH = figure;
SliderH = uicontrol('Style', 'Slider', 'Value', 1, 'Callback', {@sliderCB, TriH});
% Create a dynamic listener and store it savely inside the figure:
Listener = addlistener(SliderH, ...
'ContinuousValueChange', @(H, E) sliderCB(H, E, TriH));
UserData.SliderListener = Listener;
FigH.UserData = UserData;
end
function sliderCB(SliderH, EventData, TriH)
alpha = SliderH.Value; % Get current value of slider
set(TriH, 'FaceAlpha', alpha); % Set opacity
end
Welcome to the world of Matlab GUIs!
Roger Breton
2021-12-20
2 个评论
Jan
2021-12-21
I have the same problem since some weeks. For me it is not the time, but about half of the tries to submit an answer is not successful. Then I have to copy my asnwer, reload the page, paste it an submit it again. I did not understand the pattern yet.
I've struggeled with the interface of the forum in the past also. When it starts to annoy me, I leave the forum for some month and usually the probem was solved afterwards.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!