How to display the coordinate system of the figure (UCS icon)?

I have a 3D object having irregular shape and volume changing along the axes direction. I want to show the coordinate system icon in the figure associated with that object so that when I will rotate the object it will also change,like available in different CAD/CAE systems. How can I get this in MATLAB?

2 Comments

I normally plot this myself in the corner of the axis using the 'line' and 'text' functions. As long as you use these, and not the annotation, then they will rotate with the associated axis. You wont have the arrowheads however.
I don't know whether it can just be turned on but I have never come across it.
Thanks Henry I don't need arrowheads, do you have a written code for it?

Sign in to comment.

 Accepted Answer

Hi yu, Yes I have done it a few times. You should be aware that this will work for me because the scale on each axis is equal. For example:
%before this you have plotted a surface in axis hAx
axis(hAx,'equal')
%Get X, Y and Z data for plotting the axes...
X_range = hAx.XLim(2) - hAx.XLim(1);
X_start = hAx.XLim(1);
X_delta = X_range/20;
Y_delta = (hAx.YLim(2) - hAx.YLim(1))/20;
Y_start = hAx.YLim(1);
Z_delta = (hAx.ZLim(2) - hAx.ZLim(1))/20;
Z_start = hAx.ZLim(1);
X_Line = line(hAx,[X_start+X_delta X_start+X_delta*5],[Y_start+Y_delta Y_start+Y_delta],[Z_start+Z_delta Z_start+Z_delta]); % x Line
Y_Line = line(hAx,[X_start+X_delta X_start+X_delta],[Y_start+Y_delta Y_start+Y_delta*5],[Z_start+Z_delta Z_start+Z_delta]); % Y Line
Z_Line = line(hAx,[X_start+X_delta X_start+X_delta],[Y_start+Y_delta Y_start+Y_delta],[Z_start+Z_delta Z_start+Z_delta*5]); %Z Line
X_text = text(hAx,X_start+X_delta*6,Y_start+Y_delta,Z_start+Z_delta,'x');
Y_text = text(hAx,X_start+X_delta,Y_start+Y_delta*6,Z_start+Z_delta,'y');
Z_text = text(hAx,X_start+X_delta,Y_start+Y_delta,Z_start+Z_delta*6,'z');
You can then play around with how long you want each line, the colors of the lines and text etc... I tend to use linkprop to link the properties of the lines that need to stay the same. If you do not have equal axis scales, this method wont really work...
Henry

11 Comments

Thanks again, my scale is also same but I will surely check.
Hi Henry, I am using the following code for drawing the 3D object, kindly can you please guide me how should I adopt your code accordingly.
% load required array for drawing object
load N2_aggpores
%%%%%%%Redrawing %%%%%%%%%%%%%
Ss_P1= smooth3(N2_aggpores,'gaussian',3); %smooth surface
hiso_P1 = patch(isosurface(Ss_P1,5,'isovalue'),'FaceAlpha',0.2,... % surface view
'FaceColor',[0 1 1],...
'EdgeColor','none');
hcap_P1 = patch(isocaps(N2_aggpores,5),... % cover(cross section) view
'FaceColor','interp',...
'EdgeColor','none');
colormap copper
hold on
view(70,12)
%axis on
daspect([1,1,1])
% camzoom(1.4)
% camproj perspective
% lightangle(-40,25);
lightangle(-55,15)
camlight; lighting phong
Maybe if I could see the variable 'N2_aggpores' ?
Using the matlab example for isosurface, which I imagine is similar to your code, you just do something like this. However, if you dont use the line "axis(hAx,'square')" Im not sure all the lines will be of equal length, so you might have to play around with it a bit.
[x y z v] = flow;
q = z./x.*y.^3;
p = patch(isosurface(x, y, z, q, -.08, v));
isonormals(x,y,z,q, p)
p.FaceColor = 'interp';
p.EdgeColor = 'none';
daspect([1 1 1]); axis tight;
colormap(prism(28))
camup([1 0 0 ]); campos([25 -55 5])
camlight; lighting phong
hAx = gca;
axis(hAx,'equal')
axis(hAx,'square');
%Get X, Y and Z data for plotting the axes...
X_range = hAx.XLim(2) - hAx.XLim(1);
X_start = hAx.XLim(1);
X_delta = X_range/20;
Y_delta = (hAx.YLim(2) - hAx.YLim(1))/20;
Y_start = hAx.YLim(1);
Z_delta = (hAx.ZLim(2) - hAx.ZLim(1))/20;
Z_start = hAx.ZLim(1);
X_Line = line(hAx,[X_start+X_delta X_start+X_delta*5],[Y_start+Y_delta Y_start+Y_delta],[Z_start+Z_delta Z_start+Z_delta]); % x Line
Y_Line = line(hAx,[X_start+X_delta X_start+X_delta],[Y_start+Y_delta Y_start+Y_delta*5],[Z_start+Z_delta Z_start+Z_delta]); % Y Line
Z_Line = line(hAx,[X_start+X_delta X_start+X_delta],[Y_start+Y_delta Y_start+Y_delta],[Z_start+Z_delta Z_start+Z_delta*5]); %Z Line
X_text = text(hAx,X_start+X_delta*6,Y_start+Y_delta,Z_start+Z_delta,'x');
Y_text = text(hAx,X_start+X_delta,Y_start+Y_delta*6,Z_start+Z_delta,'y');
Z_text = text(hAx,X_start+X_delta,Y_start+Y_delta,Z_start+Z_delta*6,'z');
axis(hAx,'off');
view([29 22]);
I hope this helps.
P.S. I have edited the orignal answer as it contained some errors from when i originally copied it over...
Henry
yes sure following is the variable N2_aggpores.
So when I run this code:
% load required array for drawing object
load N2_aggpores
%%%%%%%Redrawing %%%%%%%%%%%%%
Ss_P1= smooth3(N2_aggpores,'gaussian',3); %smooth surface
hFig = figure;
hAx = gca;
hiso_P1 = patch(isosurface(Ss_P1,5,'isovalue'),'FaceAlpha',0.2,... % surface view
'FaceColor',[0 1 1],...
'EdgeColor','none');
hcap_P1 = patch(isocaps(N2_aggpores,5),... % cover(cross section) view
'FaceColor','interp',...
'EdgeColor','none');
colormap copper
hold on
view(70,12)
%axis on
daspect([1,1,1])
% camzoom(1.4)
% camproj perspective
% lightangle(-40,25);
lightangle(-55,15)
camlight; lighting phong
X_range = hAx.XLim(2) - hAx.XLim(1);
X_start = hAx.XLim(1);
X_delta = X_range/40;
Y_delta = (hAx.YLim(2) - hAx.YLim(1))/40;
Y_start = hAx.YLim(1);
Z_delta = (hAx.ZLim(2) - hAx.ZLim(1))/40;
Z_start = hAx.ZLim(1);
X_Line = line(hAx,[X_start+X_delta X_start+X_delta*5],[Y_start+Y_delta Y_start+Y_delta],[Z_start+Z_delta Z_start+Z_delta]); % x Line
Y_Line = line(hAx,[X_start+X_delta X_start+X_delta],[Y_start+Y_delta Y_start+Y_delta*5],[Z_start+Z_delta Z_start+Z_delta]); % Y Line
Z_Line = line(hAx,[X_start+X_delta X_start+X_delta],[Y_start+Y_delta Y_start+Y_delta],[Z_start+Z_delta Z_start+Z_delta*5]); %Z Line
X_text = text(hAx,X_start+X_delta*6,Y_start+Y_delta,Z_start+Z_delta,'x');
Y_text = text(hAx,X_start+X_delta,Y_start+Y_delta*6,Z_start+Z_delta,'y');
Z_text = text(hAx,X_start+X_delta,Y_start+Y_delta,Z_start+Z_delta*6,'z');
view([29 22])
axis(hAx,'off');
I get this figure. Presumably it is similar to what you want. Like i said, you can customize and decide where to place the lines by changing the X_start, X_delta values etc, but that is up to you. I hope this helps.
Henry
Thanks a lot Henry. This is exactly what I need. But when I am running this code I am get this error and it is not drawing the UCS. Any idea how to get rid of it?
Unless you have another function (or possibly a variable in the workspace) called 'line' then I am not sure. You can try using 'plot3' instead of 'line'?
Henry
Well finally it is showing the UCS but no text sorry for bothering again.
It is odd behaviour. Which version of Matlab are you using? I did this on 2016a... The way in which it is called may have changed over the years
Its finally working Henry! I have just removed 'hAx' from the text function and it works perfect. Thanks again it is of great for me. Btw my version is 2015a.
Instead of Line command you could use annotation to get the arrow heads (version 2020b)
X_Line = annotation('textarrow',[X_start+X_delta X_start+X_delta*1.5],[Y_start+Y_delta Y_start+Y_delta],'Color','k'); % x Line
Y_Line = annotation('textarrow',[X_start+X_delta X_start+X_delta],[Y_start+Y_delta Y_start+Y_delta*1.5],'Color','k'); % Y Line

Sign in to comment.

More Answers (1)

Heres what I think is a pretty nice solution. The coordinate system bases (arrows) are plotted with my plotframe function on File Exchange.
% The main axes containing the object or data of interest.
axObj = axes;
[ X, Y, Z ] = peaks( 25 );
mesh( axObj, X, Y, Z ./ 3 )
% The small, corner axes containing the coordinate system plot.
axIcon = axes( Position=[0.75 0.75 0.2 0.2] );
plotframe( Parent=axIcon, LabelBasis=true )
% Synchronise the axes cameras.
linkprop( [axObj axIcon], [ "View", "CameraUpVector" ] );
% Some beautification.
set( gcf, Color=axObj.Color )
set( axIcon, CameraViewAngleMode="manual", ...
Color="none", XColor="none", YColor="none", ZColor="none" )
set( [axObj axIcon], DataAspectRatio=[1 1 1], PlotBoxAspectRatio=[1 1 1], View=[50 30] )

Community Treasure Hunt

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

Start Hunting!