Trisurf : How to interactively change the plot opacity?

9 Ansichten (letzte 30 Tage)
Roger Breton
Roger Breton am 19 Dez. 2021
Kommentiert: Jan am 21 Dez. 2021
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 ...

Antworten (2)

Jan
Jan am 19 Dez. 2021
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 Kommentare
Roger Breton
Roger Breton am 20 Dez. 2021
I tried your code -- I had no idea it could be executable as is as a new script? I was expecting it would need to be called from some function?
When I ran your code, I saw the 3D figure appear inside the plot area and the horizontal slider control at the bottom left of the figure area -- neat!
First, I'd be curious to make the slider "wider"? Which I'm sure is possible -- a little study of the control options on my part will surely turn the parameters that determines the width in pixels or otherwise.
Second, I notice that it is "static" in the sense that, only when the mouse is released on the control, that the figure redraws itself at the new Opacity level. Which is very, very plenty for my humble needs.
Wow! Thank you!
Jan
Jan am 20 Dez. 2021
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!

Melden Sie sich an, um zu kommentieren.


Roger Breton
Roger Breton am 20 Dez. 2021
Here's the result :
And here's the code :
h1 = trisurf(k,a,b,L,'FaceColor','interp','FaceVertexCData',rgb,'EdgeColor','none', 'FaceAlpha', 0.5)
uicontrol('Style', 'Slider', 'Position',[20 20 150 20], 'Value', 1, 'Callback', {@sliderCB, h1});
function sliderCB(SliderH, EventData, h1)
alpha = SliderH.Value; % Get current value of slider
set(h1, 'FaceAlpha', alpha); % Set opacity
end
  2 Kommentare
Roger Breton
Roger Breton am 20 Dez. 2021
BTW, Matlab does not leave me much time to respond? If I take "too long" to reply, then hitting the Submit button always fail :(
Jan
Jan am 21 Dez. 2021
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by