How to shift x-axis label to right in matlab - matlab

I am trying to draw a box plot using matlab. I want to shift the position of the x-axis labels to the right. See the following code which seems not to work correctly.
positions = [2.5 3.5 4.5 5.5 6.5 7.5];
colors = [0 0 1; 0 0 1;0 0 1; 1 0 0;1 0 0;1 0 0];
x = boxplot([KA_query_length Plan_query_length Comp_query_length FR_query_length KI_query_length QA_query_length], 'Position', positions, 'Colors',colors,'whisker',0);
set(gca, 'FontSize', 20,'FontWeight','bold');
Given below is what the graph looks like at the moment.

Related

How to add legend in a highlighted graph?

I want to add legend in a graph G according to different highlighted edges. Is it possible to do it with only one graph G?
Here is a toy example to play with. I have a plot G.
adj =[0 0 1 1 1; % adjacency matrix
1 0 1 0 1;
0 1 0 1 1;
1 1 1 0 1;
0 0 1 0 0]
G = digraph(adj);
I highlighted all edges with 3 colors according to types of edges. 3 types of edges indicate there are 3 different relation between nodes in my case.
This is how I highlighted all edges:
M(:,:,1)=[0 0 1 0 0;1 0 0 0 1;0 0 0 0 0;1 0 0 0 0;0 0 1 0 0];
M(:,:,2)=[0 0 0 1 0; 0 0 1 0 0;0 1 0 0 1;0 0 0 0 0;0 0 0 0 0];
M(:,:,3)=[0 0 0 0 1; 0 0 0 0 0; 0 0 0 1 0;0 1 1 0 1;0 0 0 0 0];
The difficulty in my problem is that I have to remove vertices whose out-degree is less than some integel (say it's 2). Thus I can't plot 3 graphs independently.
rmvNode=find(outdegree(G)<2); % outdegree is the reason why single G is neccesary
adj(rmvNode,:)=[]; adj(:,rmvNode)=[];
M(:,rmvNode,:)=[]; M(rmvNode,:,:)=[];
G=digraph(adj);
Then we can plot it.
for k=1:3 %Looping depending on the third dimension
[r,c]= find(M(:,:,k)); %Finding non-zero elements
s{k}=r; t{k}=c;
end
h=plot(G);
highlight(h,s{1},t{1},'EdgeColor','r');
highlight(h,s{2},t{2},'EdgeColor','g');
highlight(h,s{3},t{3},'EdgeColor','b');
My ideal situation would be a legend like this: assign red edges to label 'type 1', assign blue edges to 'type 2', and assign green ones to 'type 3'. I want something like this:
Once more: I can't plot 3 graphs independently according to 3 pages in M, combine 3 plots together and then add a legend. Because as you can see, outdegree requires a whole graph G as input, it's not viable to divide G into G1, G2 and G3.
One way would be to manipulate the legend function by adding an invisible plot like this:
%put this at the end of your code
hold on; %to retain current plot
ax=plot(NaN,NaN,'r',NaN,NaN,'g',NaN,NaN,'b'); %plotting invisible points of desired colors
legend(ax,'Type 1','Type 2','Type 3'); %adding the legend
which gives:

Contour colors don't correspond to color bar when a surf plot is added

Below is some code that recreates my problem as simplified as I can make it. It does a subplot with two plots, you'll notice the plot on the right (contour only) has the correct correlation between the contour colors and the color bar but when a surface is added (left plot) the colors no longer match up.
Notes:
I've tried contourslice but I get the same results. I've posted the code for that below too.
How far off the colors are seems to depend on the values of the contour data itself. If you replace my contour data with peaks, it works fine. However this does not solve the underlying problem.
Code using contour:
clear all; close all; clc
%define box coordinates
bx = [0 1 1 0 0;0 1 1 0 0]-.5;
by = [0 0 1 1 0;0 0 1 1 0]-.5;
bz = [0 0 0 0 0;1 1 1 1 1]-.5;
%make contour data
[x,y] = meshgrid(-1:.5:1,-1:.5:1);
con = (x.^2+y.^2);
figure(1)
subplot(1,2,1)
box = surf(bx,by,bz); %draw box
set(box,'FaceColor',[1 1 1],'FaceAlpha',1,'EdgeAlpha',0,'EdgeColor',[.5 .5 .5])
hold on
camlight(30,70)
contour(x,y,con) %draw contour
colorbar
axis([-1 1 -1 1 -1 1])
axis equal
subplot(1,2,2)
contour(x,y,con)
axis([-1 1 -1 1])
axis equal
colorbar
set(gcf,'outerposition',[150 150 800 300])
Code using contourslice instead of contour (same problem)
clear all; close all; clc
%define box coordinates
bx = [0 1 1 0 0;0 1 1 0 0]-.5;
by = [0 0 1 1 0;0 0 1 1 0]-.5;
bz = [0 0 0 0 0;1 1 1 1 1]-.5;
x = -1:.5:1;
y = x;
z = x;
%make contour data
[xg,yg,zg] = ndgrid(x,y,z);
V = 3-(xg.^2+yg.^2+zg.^2);
figure(1)
subplot(1,2,1)
box = surf(bx,by,bz); %draw box
set(box,'FaceColor',[1 1 1],'FaceAlpha',1,'EdgeAlpha',0,'EdgeColor',[.5 .5 .5])
hold on
camlight(30,70)
contourslice(x,y,z,V,[],[],0) %draw contour
colorbar
axis([-1 1 -1 1 -1 1])
axis equal
subplot(1,2,2)
contour(x,y,V(:,:,3))
axis([-1 1 -1 1])
axis equal
colorbar
set(gcf,'outerposition',[150 150 800 300])
Thanks for your help!
Just set the caxis property as you wish:
colorbar
caxis([0 2])
...
colorbar
caxis([0 2])
The problem was probably caused, because the surf plot changed the color determining values of your plot. By setting a fixed color axis you can avoid all misinterpretations.

How to entegrate existing .m file into the simulink .mdl file

I'm using robotic toolbox by Peter Corke in Matlab . I have .m file for puma560 robot (it is for robot trajectory. The robot follows given path). When I try to use for ex. "sl_ctorque" simulink file which is in robotic toolbox(it is about computed torque method) , I couldn't entegrate my .m file into the simulink file. My .m file is given below. So if anyone know how to do this idea, I'd appreciate it. Thanks!
clear;clc;
mdl_puma560 %to create puma robot
for type=1:3 % main for loop. It turns 3 times. At first, it sets the path
% to x-y plane and draw the robot, at second for y-z plane
% and then for x-z plane
if type==1
% The path of robot for x-y plane
path=[0 0 1;0 0 0;0 2 0 ;0.5 1 0 ;1 2 0;1 0 0;1.5 0 1;1.5 0 0;
1.5 2 0;2.2 2 0;2.5 1.6 0;2.5 0.4 0;2.2 0 0;1.5 0 0;0 0 1];
elseif type==2
% Same thing as first part
path=[-0.5 0 0;0 0 0;0 0 1;0 -0.5 0.5;0 -1 1;0 -1 0;-0.5 -1.2 0;0 -1.2 0;
0 -1.2 1;0 -1.7 1;0 -2 0.7;0 -2 0.3;0 -1.7 0;0 -1.2 0];
elseif type==3
% Same thing as first and second part
path=[0 -0.5 0;0 0 0;0 0 1;0.5 0 0.5;1 0 1;1 0 0;1.3 -0.5 0;1.3 0 0;
1.3 0 1;1.7 0 1;2 0 0.7;2 0 0.3;1.7 0 0;1.3 0 0];
end
% I created a trajectory
p=mstraj(path, [15 15 15], [], [1 0 1], 0.02 , 0.2);
% [15 15 15] means the maximum speed in x,y,z directions.
% [1 0 1] means the initial coordinates
% 0.02 means acceleration time
% 0.2 means smoothness of robot
numrows(p)*0.2; % 200 ms sample interval
Tp=transl(0.1*p); % Scale factor of robot
Tp=homtrans( transl(0.4,0,0),Tp); % Origin of the letter
q=p560.ikine6s(Tp); % The inverse kinematic
for i=1:length(q)
% q matrix has 280 rows and 6 columns. So this for loop turns 280 times
% At every turns , it plots one part of movement. q(1,:), q(2,:), ...
p560.plot(q(i,:))
end
end
You need to write your m file as a function and then use the MATLAB Function Block.
The MATLAB Function block allows you to add MATLABĀ® functions to SimulinkĀ® models for deployment to desktop and embedded processors. This capability is useful for coding algorithms that are better stated in the textual language of the MATLAB software than in the graphical language of the Simulink product.
Then you can open the block as paste your function:
to see an example check out this page.

Entegrating existing .mfile to the .mdl simulink

I convert my .m file above as a function like below , my input is nothing and my output is q. But I have a problem. When I put my created function block to the simulink and connect to the display screen , matlab gives me some errors like;
*Try and catch are not supported for code generation.
Function 'tb_optparse.m' (#80.5667.6083), line 157, column 25:
"try"
Launch diagnostic report.*
Function call failed.
Function 'MATLAB Function' (#94.848.897), line 37, column 3:
"mstraj(path, [15 15 15], [], [1 0 1], 0.02 , 0.2)"
Launch diagnostic report.
Errors occurred during parsing of MATLAB function 'MATLAB Function'(#93)
How can I fix these errors? Thanks
function output = fcn()
%mdl_puma560 %to create puma robot
for type=1:3 % main for loop. It turns 3 times. At first, it sets the path
% to x-y plane and draw the robot, at second for y-z plane
% and then for x-z plane
if type==1
% The path of robot for x-y plane
path=[0 0 1;0 0 0;0 2 0 ;0.5 1 0 ;1 2 0;1 0 0;1.5 0 1;1.5 0 0;
1.5 2 0;2.2 2 0;2.5 1.6 0;2.5 0.4 0;2.2 0 0;1.5 0 0;0 0 1];
elseif type==2
% Same thing as first part
path=[-0.5 0 0;0 0 0;0 0 1;0 -0.5 0.5;0 -1 1;0 -1 0;-0.5 -1.2 0;0 -1.2 0;
0 -1.2 1;0 -1.7 1;0 -2 0.7;0 -2 0.3;0 -1.7 0;0 -1.2 0];
elseif type==3
% Same thing as first and second part
path=[0 -0.5 0;0 0 0;0 0 1;0.5 0 0.5;1 0 1;1 0 0;1.3 -0.5 0;1.3 0 0;
1.3 0 1;1.7 0 1;2 0 0.7;2 0 0.3;1.7 0 0;1.3 0 0];
end
% I created a trajectory
p=mstraj(path, [15 15 15], [], [1 0 1], 0.02 , 0.2);
% [15 15 15] means the maximum speed in x,y,z directions.
% [1 0 1] means the initial coordinates
% 0.02 means acceleration time
% 0.2 means smoothness of robot
numrows(p)*0.2; % 200 ms sample interval
Tp=transl(0.1*p); % Scale factor of robot
Tp=homtrans( transl(0.4,0,0),Tp); % Origin of the letter
q=p560.ikine6s(Tp) ; % The inverse kinematic
% for i=1:length(q)
% %q matrix has 280 rows and 6 columns. So this for loop turns 280 times
% % At every turns , it plots one part of movement. q(1,:), q(2,:), ...
%
% p560.plot(q(i,:))
%
% end
end
output=q;
Well as the error message says, it looks like your function mstraj is calling try/catch which isn't supported for code generation (MATLAB functions in Simulink are first converted to C code when you run the model).
Have a look at Call MATLAB Functions in the documentation for ways to work around this using coder.extrinsic. Extrinsic functions return data of type mxArray so you will need to convert it to whatever the data type of p (see Converting mxArrays to Known Types in the documentation page above).
In you case, it would probably look something like:
function output = fcn()
coder.extrinsic('mstraj');
% etc...
p = 0; % Define p as a scalar of type double (change to required data type if not appropriate)
p=mstraj(path, [15 15 15], [], [1 0 1], 0.02 , 0.2);
% etc...

How can I display a 2D binary matrix as a black & white plot?

I have a 2D binary matrix that I want to display as a black and white plot. For example, let's say I have a 4-by-4 matrix as follows:
1 1 0 1
0 0 1 0
1 1 0 1
1 0 0 0
How can this be plotted as a black and white matrix? Some of my input binary matrices are of size 100-by-9, so I would ideally need a solution that generalizes to different sized matrices.
If you want to make a crossword-type plot as shown here (with grid lines and black and white squares) you can use the imagesc function, a gray colormap, and modify the axes properties like so:
mat = [1 1 0 1; 0 0 1 0; 1 1 0 1; 1 0 0 0]; % Your sample matrix
[r, c] = size(mat); % Get the matrix size
imagesc((1:c)+0.5, (1:r)+0.5, mat); % Plot the image
colormap(gray); % Use a gray colormap
axis equal % Make axes grid sizes equal
set(gca, 'XTick', 1:(c+1), 'YTick', 1:(r+1), ... % Change some axes properties
'XLim', [1 c+1], 'YLim', [1 r+1], ...
'GridLineStyle', '-', 'XGrid', 'on', 'YGrid', 'on');
And here's the image you should get:
I'm not sure if I got your question right, but you may try the image function, like this:
A = [ 1 1 0; 1 0 1; 1 1 1 ];
colormap([0 0 0; 1 1 1 ]);
image(A .* 255);
Try the spy function to start with perhaps.