Matlab: use plot and semilogy in same graph (Matlab 2014) - matlab

I want to use plot and semilog in the same graph so as to compare results. I get only the one waveform though using hold on function. What can I do ? I cant use yyaxis left cause my matlab is lower than 2016. Any help?
Here is my code:
figure
semilogy(VG, ID3)
hold on
plot(VG, ID3)
hold off

A possible solution to have both the linear and the semilog chart in the same graph could be to create two overlapping graphs:
on the first you can plot the data with the linear scale
on the second you can plot the data with the logarithmic scale either on the x or axis
The main steps are:
create a figure
create the first axes in the figure
set the color of the axes to the same color used for the data, this will help recognising the data
plot the data with plot
create the second axes in the figure and set its size equal to the one of the first axes (now the secon axes is the one "active")
move the X axis location to the top of the chart
move the Yaxis location to the right of the chart
plot the data with semilogy
set the color of the axes to the same color used for the data, this will help recognising the data
add the title, the legend and the x / y labels
Since we have changed the location of the X and Y axis of the second axes, we need to adjust the dimenisons of the figure and of the axes so thay fit toghter
Now you can fix the problem of the different grid of the two axes:
to do that, you can add a menu item to the menu bar (with the uimenu function) to toggle the grid
In the following, a possible implementation of the suggested approach.
Since you have not specified if you use R2014a or R014b, in the code below you can find both the way to set the properties of the figure and axes:
the "old" way: using get / set
the "new" one dot notation available from R2014b
(the last one is "commented")
% Define input data
x=linspace(0,30,30)
y=rand(30, 1);
% Cretate a Figure
f=figure('units','normalized')
% Create the first axes in the figure
ax1=axes
% Plot the data with "PLOT"
ph=plot(x,y,'r')
% Set the axis labeks
xlabel('X data')
ylabel('Y data, LIN mode')
% Get the position of the first axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1_pos=ax1.Position
ax1_pos=get(ax1,'position')
% Set the color of the fist axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1.XColor='r'
% % % % % % % % % % % % % % % % % % % % % % % % ax1.YColor='r'
set(ax1,'xcolor','r','ycolor','r')
% Add the second axes in the figure
ax2=axes('position',ax1_pos)
% Plot the data with SEMILOGY
slh=semilogy(x,y,'b')
% Set the ylabel
ylabel('Y data, LOG mode')
% Set the title of the chrt
title('Linear and Semilog Plot')
% Move the X axis location to the top of the chart
% % % % % % % % % % % % % % % % % % % % % % % % ax2.XAxisLocation='top'
set(ax2,'XAxisLocation','top')
% Move the XYaxis location to the right of the chart
% % % % % % % % % % % % % % % % % % % % % % % % ax2.YAxisLocation='right'
set(ax2,'YAxisLocation','right')
% Set the color of the second axes to transparent
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.Color='none'
set(ax2,'color','none')
% Set the color of the second axes
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.XColor='b'
% % % % % % % % % % % % % % % % % % % % % % % % % ax2.YColor='b'
set(ax2,'xcolor','b','ycolor','b')
% Add the lgend to the chart
legend([ph slh],'plot','semilogy','location','best')
% Adjust the size of the the first axes
% % % % % % % % % % % % % % % % % % % % % % % % ax1.Position=[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9]
set(ax1,'position',[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9])
% et the size of the second axes as per the first one
ax2.Position=ax1.Position
set(ax2,'position',[ax1_pos(1) ax1_pos(2) ax1_pos(3)*.9 ax1_pos(4)*.9])
% Adjust the size of the figure
% % % % % % % % % % % % % % % % % % % % % % % % % % % fp=f.Position
fp=get(f,'position')
% % % % % % % % % % % % % % % % % % % % % % % % % % % f.Position=[0.1,0.1,fp(3)*1.2,fp(4)*1.2]
set(f,'position',[0.1,0.1,fp(3)*1.2,fp(4)*1.2])
% Add the menu to manage the grid
mh=uimenu('Label','Grid manag.')
m1h=uimenu(mh,'Label','Linear Grid','callback', ...
'axes(ax1);grid;axes(ax2);if(strcmp(m1h.Checked,''off'')),m1h.Checked=''on'';else,m1h.Checked=''off'';end')
m2h=uimenu(mh,'Label','Log Grid','callback', ...
'axes(ax2);grid;if(strcmp(m2h.Checked,''off'')),m2h.Checked=''on'';else,m2h.Checked=''off'';end')

I often have to make figures comparing data in linear and log space. Stacking the plots using subplot() gives a nice visual. Example:
% set up dummy data that is evenly-space in logspace
x = 10.^((linspace(log10(.01), log10(10),20))');
y = rand(20, 1);
% finish setup
figure
subplot(2,1,1)
plot(x, y, 'DisplayName', 'MyData')
title('Plot with Linear Axes')
xlabel('X-Axis')
ylabel('Y-Axis')
grid on
set(legend, 'Location', 'best')
subplot(2,1,2)
semilogx(x, y, 'DisplayName', 'MyData')
title('Plot with LogX Axis')
xlabel('LogX-Axis')
ylabel('Y-Axis')
grid on
set(legend, 'Location', 'best')
The above produces this:

Related

SeDuMi Matlab Code for Optimization problem

have this optimization Problem and need to implement this problem using SeDuMi Technique in MATLAB. Here, Q_i = h_i * h_i^(H). and h_i are randomly generated vector. I would like someone to help me with the matlab code for the implementation of this optimization problem in SeDuMi:
Matlab code, which i have been working on:
% % % % % % % % % % % % % % % % % % % % % % % % % % % % %
clear all; clc
N = 10; % Number of antennas # BS
M = 5; % Number of single antenna User
H = (randn(N,M) + 1j*randn(N,M)) *sqrt(0.5);
Q = zeros(N,N,M);
vecQs= [];
for k = 1:M
Q(:,:,k) =H(:,k)'*H(:,k);
vecQs = [vecQs,vec(Q(k).')];
end
A=[-eye(M),vecQs.'];
b=ones(M,1);
c=[zeros(M,1);vec(eye(N))];
K.l=M;
K.s=N;
K.scomplex=1;
[x_opt,y_opt,info]=sedumi(A,b,c,K);
Xopt=mat(x_opt(M+1:end));

"Not enough input arguments" when publishing MATLAB commands

I've written the following code:
function [a1,b1,a0,b0] = Conformal_2D(x_input,y_input,X_output,Y_output)
%%
% Calculates parameters $a,b,a_0,b_0$ in the following equation using least
% squares
%%
%
% $$X=a_1x+b_1y+a_0$$
%
% $$X=-b_1x+a_1y+b_0$$
%%
% *Arguments:*
%
% x_input is a $n\times 1$ matrix containing x coordinate of control points
% in the input space
%
% y_input is a $n\times 1$ matrix containing y coordinate of control points
% in the input space
%
% x_output is a $n\times 1$ matrix containing x coordinate of control points
% in the output space
%
% y_output is a $n\times 1$ matrix containing y coordinate of control points
% in the output space
%%
NumberOfPoints = size(x_input,1);
A = zeros(2*NumberOfPoints,1); % Coefficient matrix in AX = L
L = zeros(2*NumberOfPoints,1); % Right-hand matrix in AX = L
for i = 1:NumberOfPoints
A(2*i-1,1:4) = [x_input(i,1) y_input(i,1) 1 0];
end
end
When I press 'Publish' button, I get:
What's the problem with that line?
Not sure if this is the best workaround, but I managed to publish the function with the following script. Of course, you will use your own entries for x_input, y_inpyt, etc. Make sure that you save the following script in a file not named Conformal_2D.m.
Conformal_2D([3;2;1],[5;6;7],[11;12;13],[14;15;16]);
function [a1,b1,a0,b0] = Conformal_2D(x_input,y_input,X_output,Y_output)
%%
% Calculates parameters $a,b,a_0,b_0$ in the following equation using least
% squares
%%
%
% $$X=a_1x+b_1y+a_0$$
%
% $$X=-b_1x+a_1y+b_0$$
%%
% *Arguments:*
%
% x_input is a $n\times 1$ matrix containing x coordinate of control points
% in the input space
%
% y_input is a $n\times 1$ matrix containing y coordinate of control points
% in the input space
%
% x_output is a $n\times 1$ matrix containing x coordinate of control points
% in the output space
%
% y_output is a $n\times 1$ matrix containing y coordinate of control points
% in the output space
%%
NumberOfPoints = size(x_input,1);
A = zeros(2*NumberOfPoints,1); % Coefficient matrix in AX = L
L = zeros(2*NumberOfPoints,1); % Right-hand matrix in AX = L
for i = 1:NumberOfPoints
A(2*i-1,1:4) = [x_input(i,1) y_input(i,1) 1 0];
end
end
When publishing, MATLAB runs the code by default. Of course, your function has input arguments that will not be set in this case (see this other question).
There are a few ways around this. Juanchito's answer illustrates one: don't publish a function, publish a script.
Alternatively, publish the function without executing it:
publish('Conformal_2D.m','evalCode',false)
Or, give MATLAB the right declarations to evaluate before executing the function's content:
publish('Conformal_2D.m','codeToEvaluate','x_input=1; y_input=2; X_output=3; Y_output=4')
(do adjust those values, of course).

Add flexible legend in MATLAB

I am trying to add a flexible legend (reduce the length of the line and suppress the unnecessary space between the line and the text) to my figure, I used the steps from another post : advanced plotting (legend manipulation) in Matlab, the code it work fine but when I try to show the figure in the full screen after the legend modification, Everything I have done is meaningless.
x = randn(6,20);
figure(2)
hax = gca;
plot(x(1,:),'--k','linewidth',1.5);
hold on;
plot(x(2,:),'b','linewidth',1.5);
% hold on;
plot(x(3,:),'g','linewidth',1.5);
% hold on;
plot(x(4,:),'r','linewidth',1.5);
% hold on;
plot(x(5,:),'c','linewidth',1.5);
% hold on;
plot(x(6,:),':r','linewidth',1.5);
ylabel('states','fontsize',14); xlabel('time(s)','fontsize',10);
%legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14);
%
% New call "legend"
%
[leg_h,leg_item_h,~,~]=legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14);
%
% legendshrink(0.8,[]);
%Fig_legend = legend('Taylor','Euler','LLscheme','LLscheme1');
%set(Fig_legend,'FontSize',7)
grid(hax,'on')
% axis(hax,'tight')
set(hax,'box','on','Layer','top');
set(hax,'tickdir','out')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% GENERATION OF THE LEGEND %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define a scale factor fot the lines
line_scale_factor=1.4;
% Define a scale factor fot the lines
text_scale_factor=1.35;
% Get the "Position" of the legend
orig_leg_pos=get(leg_h,'position')
% Get the number on objects in the legend
n_obj=length(leg_item_h);
% Extract the "Line" object
line_obj=leg_item_h(n_obj/3+1:2:n_obj);
% Get the "LineStyle" of each "Line" in the legend
l_style=get(line_obj,'LineStyle')
% Get the "Color" of each "Line" in the legend
l_col=cell2mat(get(line_obj,'color'))
% Get the "XData" and "YData" of the "Lines" in the legend
leg_x_data=cell2mat(get(line_obj,'xdata'))
leg_y_data=cell2mat(get(line_obj,'ydata'))
% Get the handle of the "Text" of the items in the legend
leg_t=leg_item_h(1:n_obj/3)
% Get the "Text" of the items in the legend
str=get(leg_t,'string')
% Get the "Position" of each "Text" item in the legend
tx=cell2mat(get(leg_t,'position'))
% Delete the original legend
delete(leg_h)
% Create an axes with the same position and size of the original legend
ax=axes('position',orig_leg_pos,'xlim',[0 1],'ylim',[0 1], ...
'xtick',[],'ytick',[],'box','on')
hold on
% Add the legend items to the axes
for i=1:n_obj/3
% Add the lines with the original settings (color, style, ...)
plot([leg_x_data(i,1) leg_x_data(i,2)/line_scale_factor],leg_y_data(i,:),'color',l_col(i,:), ...
'linestyle',l_style{i}, ...
'linewidth',1.4)
% Add the text
th(i,:)=text(leg_x_data(i,2)/text_scale_factor,tx(i,2),0,str{i},'fontsize',9, ...
'unit','normalized')
end
% Get the maximun extent of the lagend "Text"
tx_max_ext=max(reshape([th(:).Extent],4,6)');
% Evaluate the axis scaling factor
tx_r_1=tx_max_ext(3)+leg_x_data(i,2)/line_scale_factor
% Get the axes position
axp=ax.Position
% Resize the axes width
ax.Position=[axp(1) axp(2) axp(3)*tx_r_1 axp(4)]
You can achieve your goal by moving the section of the above code that create the "new legend" into a ResizeFcn of the figure.
This allow adequating the size of the axes based legend each time the size of the figure changes.
To do so, you have to modify your code this way:
1) assign a tag to the figure, This will be used in the ResizeFcn to access to the figure:
cf=figure('tag','res_leg')
2) use guidata to store the original legend handles into the figure object
% Set in the FIGURE GUIDATA the legend handles
my_guidata=guidata(cf)
my_guidata.leg_h=leg_h;
my_guidata.leg_item_h=leg_item_h
% Store the GUIDATA data
guidata(cf,my_guidata)
3) Assign the ResizeFcn to the figure
set(cf,'ResizeFcn',['doResizeFcn(''' get(gcf,'tag') ''')'])
4) Define the ResizeFcn
just move the code you have now in the section in the section % GENERATION OF THE LEGEND % in a function named as the ResizeFcn (in this case doResizeFcn)
Make the following update to the above mentioned code (in the order they are in in the code):
4.1) Get the handle of the figure
4.2) Retrie the figure data using guidata
4.3) Check if the figure already contains the axes used in place of the legend; if so, delete it
4.4) Retrie the original legend data using guidata
4.5) Store in the figure guidata the handle of the axes created to replace the original legend
4.6) Store the figure's guidata
In the following the whole updated code - the above described steps are marked in the code as
%%%%%%%%%%%%
% STEP 4.1 %
%%%%%%%%%%%%
Original code for the plotting
x = randn(6,20);
%%%%%%%%%%
% STEP 1 %
%%%%%%%%%%
% Create the FIGURE and assign a TAG to it (to be used by FINDOBJ)
cf=figure('tag','res_leg')
hax = gca;
plot(x(1,:),'--k','linewidth',1.5);
hold on;
plot(x(2,:),'b','linewidth',1.5);
% hold on;
plot(x(3,:),'g','linewidth',1.5);
% hold on;
plot(x(4,:),'r','linewidth',1.5);
% hold on;
plot(x(5,:),'c','linewidth',1.5);
% hold on;
plot(x(6,:),':r','linewidth',1.5);
ylabel('states','fontsize',14); xlabel('time(s)','fontsize',10);
%legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14);
%
% New call "legend"
%
[leg_h,leg_item_h,~,~]=legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14);
%
% legendshrink(0.8,[]);
%Fig_legend = legend('Taylor','Euler','LLscheme','LLscheme1');
%set(Fig_legend,'FontSize',7)
grid(hax,'on')
% axis(hax,'tight')
set(hax,'box','on','Layer','top');
set(hax,'tickdir','out')
%%%%%%%%%%
% STEP 2 %
%%%%%%%%%%
% Set in the FIGURE GUIDATA the legend handles
my_guidata=guidata(cf)
my_guidata.leg_h=leg_h;
my_guidata.leg_item_h=leg_item_h
% Store the GUIDATA data
%%%%%%%%%%
% STEP 3 %
%%%%%%%%%%
guidata(cf,my_guidata)
%%%%%%%%%%
% STEP 4 %
%%%%%%%%%%
% Assign a RESIZE function the the figure
set(cf,'ResizeFcn',['doResizeFcn(''' get(gcf,'tag') ''')'])
Figure's ResizeFnc
function doResizeFcn(str)
%%%%%%%%%%%%
% STEP 4.1 %
%%%%%%%%%%%%
% Get the handle of the calling FIGURE
curr_fig=findobj('tag',str);
%%%%%%%%%%%%
% STEP 4.2 %
%%%%%%%%%%%%
% Retrieve the GUIDATA
my_guidata=guidata(curr_fig)
%%%%%%%%%%%%
% STEP 4.3 %
%%%%%%%%%%%%
% Check if the RESIZE function has been already called
if(isfield(my_guidata,'ax'))
% if so, delete the previously created axes (used in place of the
% legend)
delete(my_guidata.ax)
end
%%%%%%%%%%%%
% STEP 4.4 %
%%%%%%%%%%%%
% Get the old legend handles to retrieve the original legend data
leg_h=my_guidata.leg_h;
leg_item_h=my_guidata.leg_item_h
line_scale_factor=1.4;
% Define a scale factor fot the lines
text_scale_factor=1.35;
% Get the "Position" of the legend
orig_leg_pos=get(leg_h,'position')
% Get the number on objects in the legend
n_obj=length(leg_item_h);
% Extract the "Line" object
line_obj=leg_item_h(n_obj/3+1:2:n_obj);
% Get the "LineStyle" of each "Line" in the legend
l_style=get(line_obj,'LineStyle')
% Get the "Color" of each "Line" in the legend
l_col=cell2mat(get(line_obj,'color'))
% Get the "XData" and "YData" of the "Lines" in the legend
leg_x_data=cell2mat(get(line_obj,'xdata'))
leg_y_data=cell2mat(get(line_obj,'ydata'))
% Get the handle of the "Text" of the items in the legend
leg_t=leg_item_h(1:n_obj/3)
% Get the "Text" of the items in the legend
str=get(leg_t,'string')
% Get the "Position" of each "Text" item in the legend
tx=cell2mat(get(leg_t,'position'))
% Delete the original legend
% % % delete(leg_h)
leg_h.Visible='off'
% Create an axes with the same position and size of the original legend
ax=axes('position',orig_leg_pos,'xlim',[0 1],'ylim',[0 1], ...
'xtick',[],'ytick',[],'box','on')
%%%%%%%%%%%%
% STEP 4.5 %
%%%%%%%%%%%%
% Store in the FIGURE GUIDATA the axes handle
my_guidata.ax=ax;
%%%%%%%%%%%%
% STEP 4.6 %
%%%%%%%%%%%%
% Store the FIGURE GUIDATA
guidata(curr_fig,my_guidata)
hold on
% Add the legend items to the axes
for i=1:n_obj/3
% Add the lines with the original settings (color, style, ...)
plot([leg_x_data(i,1) leg_x_data(i,2)/line_scale_factor],leg_y_data(i,:),'color',l_col(i,:), ...
'linestyle',l_style{i}, ...
'linewidth',1.4)
% Add the text
th(i,:)=text(leg_x_data(i,2)/text_scale_factor,tx(i,2),0,str{i},'fontsize',9, ...
'unit','normalized')
end
% Get the maximun extent of the lagend "Text"
tx_max_ext=max(reshape([th(:).Extent],4,6)');
% Evaluate the axis scaling factor
tx_r_1=tx_max_ext(3)+leg_x_data(i,2)/line_scale_factor
% Get the axes position
axp=ax.Position
% Resize the axes width
ax.Position=[axp(1) axp(2) axp(3)*tx_r_1 axp(4)]
end
Hope this helps,
Qapla'

bin2mat running slow need a faster solution

I am using bin2mat function from matlab file exchange, for some reason it runs very slow. Is it possible to make it run faster or is there an alternative? I am trying: zC = bin2mat(s.X,s.Y,s.Z,xC,yC); I am not sure where it gets bogged down. I need to do this on point cloud data to calculate volume and such.
Here is the code:
function ZG = bin2mat(x,y,z,XI,YI,varargin)
% BIN2MAT - create a matrix from scattered data without interpolation
%
% ZG = BIN2MAT(X,Y,Z,XI,YI) - creates a grid from the data
% in the (usually) nonuniformily-spaced vectors (x,y,z)
% using grid-cell averaging (no interpolation). The grid
% dimensions are specified by the uniformily spaced vectors
% XI and YI (as produced by meshgrid).
%
% ZG = BIN2MAT(...,#FUN) - evaluates the function FUN for each
% cell in the specified grid (rather than using the default
% function, mean). If the function FUN returns non-scalar output,
% the output ZG will be a cell array.
%
% ZG = BIN2MAT(...,#FUN,ARG1,ARG2,...) provides aditional
% arguments which are passed to the function FUN.
%
% EXAMPLE
%
% %generate some scattered data
% [x,y,z]=peaks(150);
% ind=(rand(size(x))>0.9);
% xs=x(ind); ys=y(ind); zs=z(ind);
%
% %create a grid, use lower resolution if
% %no gaps are desired
% xi=min(xs):0.25:max(xs);
% yi=min(ys):0.25:max(ys);
% [XI,YI]=meshgrid(xi,yi);
%
% %calculate the mean and standard deviation
% %for each grid-cell using bin2mat
% Zm=bin2mat(xs,ys,zs,XI,YI); %mean
% Zs=bin2mat(xs,ys,zs,XI,YI,#std); %std
%
% %plot the results
% figure
% subplot(1,3,1);
% scatter(xs,ys,10,zs,'filled')
% axis image
% title('Scatter Data')
%
% subplot(1,3,2);
% pcolor(XI,YI,Zm)
% shading flat
% axis image
% title('Grid-cell Average')
%
% subplot(1,3,3);
% pcolor(XI,YI,Zs)
% shading flat
% axis image
% title('Grid-cell Std. Dev.')
%
% SEE also RESHAPE ACCUMARRAY FEVAL
% A. Stevens 3/10/2009
% astevens#usgs.gov
%check inputs
error(nargchk(5,inf,nargin,'struct'));
%make sure the vectors are column vectors
x = x(:);
y = y(:);
z = z(:);
if all(any(diff(cellfun(#length,{x,y,z}))));
error('Inputs x, y, and z must be the same size');
end
%process optional input
fun=#mean;
test=1;
if ~isempty(varargin)
fun=varargin{1};
if ~isa(fun,'function_handle');
fun=str2func(fun);
end
%test the function for non-scalar output
test = feval(fun,rand(5,1),varargin{2:end});
end
%grid nodes
xi=XI(1,:);
yi=YI(:,1);
[m,n]=size(XI);
%limit values to those within the specified grid
xmin=min(xi);
xmax=max(xi);
ymin=min(yi);
ymax=max(yi);
gind =(x>=xmin & x<=xmax & ...
y>=ymin & y<=ymax);
%find the indices for each x and y in the grid
[junk,xind] = histc(x(gind),xi);
[junk,yind] = histc(y(gind),yi);
%break the data into a cell for each grid node
blc_ind=accumarray([yind xind],z(gind),[m n],#(x){x},{NaN});
%evaluate the data in each grid using FUN
if numel(test)>1
ZG=cellfun(#(x)(feval(fun,x,varargin{2:end})),blc_ind,'uni',0);
else
ZG=cellfun(#(x)(feval(fun,x,varargin{2:end})),blc_ind);
end
It is slower on these two steps for one run it took:
ZG=cellfun(#(x)(feval(fun,x,varargin{2:end})),blc_ind); took 33 secs
blc_ind=accumarray([yind xind],z(gind),[m n],#(x){x},{NaN}); took 10 secs
You can change blc_ind = ... to
ZG=accumarray([yind xind],z(gind),[m n],#mean,NaN);
and delete other codes form here so the is no need to if numel(test)>1....

index out of bounds in matlab [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am not able to get how to run it, can anyone help me on this please.
I am receiving this error :
K>> [minutiae_valid]=postprocess_TICO(B,im,~K)
??? Attempted to access wind(273,386); index out of bounds because size(wind)=[368,385].
Error in ==> postprocess_TICO at 214
if (thinned(p,q)==1)&&(wind(p,q)==0)
postprocess_Tico
function [minutiae_valid]=postprocess_TICO(minutiae_valid,im1,thinned)
% clc;
% close all;
% clear all;
cell=16;%total no. of cells needed in quantization;
% im1= imread('E:\FINGERPRINT DATABASE\FVC2002\Dbs\Db1_a\16_6.tif');
wl=(33-1)/2;
% no=20;
% no2=20;
% nox = 0;
% noy = 0; %to choose border area
%
m=size(im1,1);
n=size(im1,2);
%m=(round(m/cell))*cell;
%n=(round(n/cell))*cell;
% im1=imresize(im1,[m n]);
% im=double(im1);
% figure;
% subplot(1,2,1);
% imshow(uint8(im));
% title('original input image');
%
% %Identify ridge-like regions and normalise image--------------------------
% blksze = 10;
% thresh =30;
% M0=100;
% STD0=150;
% [thinim, mask] = imgenhance(im, blksze, M0, STD0);
% subplot(1,2,2); subimage((thinim));
% title('Improved thinned input image', 'FontSize', 12);
% INPUT_IMG=thinim;
%
% %minutiae extraction
% thinned= INPUT_IMG;
% img = INPUT_IMG;
% [minutiae, minutiae_img, combined] = findminutiae(thinned, img);
% totminu_I = size(minutiae,1)
% figure,
% subplot(1,2,1), subimage(combined), title('minutiae before postprocess.')
% %______________________________________________________________
% %TO FIND BOUNDARY OF INPUT IMAGE AS WELL AS REGION OF FALSE MINU
% mask_input=mask;
% border=uint8(zeros(m,n));
%
% [m,n]=size(mask);
% validr=uint8(zeros(m,n));
% thin_temp=thinned;
% for i=1:m
% for j=2:n-1
% if((mask_input(i,j-1)==0)&&(mask_input(i,j)==1)&&(j<=n-no))
% border(i,j)=1;
% validr(i,j-nox:j+no)=1;
% thin_temp(i,j-nox:j+no)=1;
% end
%
% if ((mask_input(i,j+1)==0)&&(mask_input(i,j)==1)&&(j>no))
% border(i,j)=1;
% validr(i,j+nox:-1:j-no)=1;
% thin_temp(i,j+nox:-1:j-no)=1;
% end
%
% end
% end
%
% for i=1:m
% for j=1:n-1:n
% if (mask_input(i,j)==1)
% if (j==1)
% border(i,j)=1;
% validr(i,j:j+no)=1;
% thin_temp(i,j:j+no)=1;
% end
% if (j==n)
% border(i,j)=1;
% validr(i,j:-1:j-no)=1;
% thin_temp(i,j:-1:j-no)=1;
% end
% end
% end
% end
%
%
%
% for j=1:n
% for i=2:m-1
% if((mask_input(i-1,j)==0)&&(mask_input(i,j)==1)&&(i<=m-no))
% border(i,j)=1;
% validr(i-noy:i+no,j)=1;
% thin_temp(i-noy:i+no,j)=1;
% end
% if((mask_input(i+1,j)==0)&&(mask_input(i,j)==1)&&(i>no))
% border(i,j)=1;
% validr(i+noy:-1:i-no,j)=1;
% thin_temp(i+noy:-1:i-no,j)=1;
% end
%
% end
% end
%
% for j=1:n
% for i=1:m-1:m
% if (mask_input(i,j)==1)
% if (i==1)
% border(i,j)=1;
% validr(i:i+no,j)=1;
% thin_temp(i:i+no,j)=1;
% else validr(i:-1:i-no,j)=1;
% thin_temp(i:-1:i-no,j)=1;
% end
% end
% end
% end
%
% %___________________________________________________________________
% % removing minu at the border of roi
% for x=1:size(thinned,1)
% for y=1:size(thinned,2)
% if ((validr(x,y)==1)&&(thinned(x,y)==1))
% combined(x,y,:)=[255,255,255];
% minutiae_img(x,y,:)=[0,0,0];
%
% end
% end
% end
%
% for i=1:totminu_I
% x=minutiae(i,1);
% y=minutiae(i,2);
% if ((validr(x,y)==1)&&(thinned(x,y)==1))
% minutiae(i,:)=0;
% end
% end
% %__________________________
%
% subplot(1,2,2), subimage(combined), title('after boundary effect ')
% minu_count=1;
% minutiae_valid(minu_count, :) = [0,0,0,0]; %to make x, y, CN, theta value of minutiae not in border region.
% for i=1:totminu_I
% CN=minutiae(i,3);
% if (CN~=0)
% minutiae_valid(minu_count, :) = minutiae(i,:);
% minu_count = minu_count + 1;
% end
% end
% totminu_I= minu_count-1;
k=size(minutiae_valid);
totminu_I=k(1,1);
%FALSE RIDGE BIFURCATION
for i=1:totminu_I
if minutiae_valid(i,3)==3
wind=ones(m,n);
xi= minutiae_valid(i,1);
yi=minutiae_valid(i,2);
x=xi;
y=yi;
wind(xi-wl:xi+wl,yi-wl:yi+wl)=0; %window of size 25 cross 25
wind(xi,yi)=-1;
%mark '1', '2', '3'.
value=0;
r=x-1;
for c=y-1:y+1
if (thinned(r,c)==1)&&(wind(r,c)==0)
value=value+1;
wind(r,c)=value;
end
end
c=y+1;
for r=x-1:x+1
if (thinned(r,c)==1)&&(wind(r,c)==0)
value=value+1;
wind(r,c)=value;
end
end
r=x+1;
for c=y+1:-1:y-1
if (thinned(r,c)==1)&&(wind(r,c)==0)
value=value+1;
wind(r,c)=value;
end
end
c=y-1;
for r=x+1:-1:x-1
if (thinned(r,c)==1)&&(wind(r,c)==0)
value=value+1;
wind(r,c)=value;
end
end
for value=1:3 %search '1'/'2/'3' value pixel arond the minutia point in the window.
for p=x-1:x+1
for q=y-1:y+1
if wind(p,q)==value
xi=p;
yi=q;
end
end
end
cnt=0;
while(xi>x-wl)&&(xi<x+wl)&&(yi>y-wl)&&(yi<y+wl) %mark '1'/'2'/'3' to the adjacent connected pixels
k=0;
for p=xi-1:xi+1
for q=yi-1:yi+1
if (thinned(p,q)==1)&&(wind(p,q)==0)
wind(p,q)=value;
k=k+1;
if k==1
x1=p;
y1=q;
end
if k==2 %more than one connected pixel
cnt=cnt+1;
x2=p;
y2=q;
t1=x1;
x1=x2;
x2=t1;
t2=y1;
y1=y2;
y2=t2;
%___________
xi=x2;
yi=y2;
while(xi>x-wl)&&(xi<x+wl)&&(yi>y-wl)&&(yi<y+wl)
k=0;
flag_see=0;
for p=xi-1:xi+1
for q=yi-1:yi+1
if (thinned(p,q)==1)&&(wind(p,q)==0)
wind(p,q)=value;
flag_see=1;
x3=p;
y3=q;
k=k+1;
end
end
end
if(flag_see==1)
xi=x3;
yi=y3;
else
xi=p;
yi=q;
end
if k==0
break
end
end
% xi=x1;
% yi=y1;
end
end
end
end
xi=x1;
yi=y1;
if k==0
break
end
end
end
% figure,subimage(wind);
%to count 0-1, 0-2, 0-3 transition around the boundary clkwise
T=1;
for v=1:3
T0v=0;
r=x-wl;
for c=y-wl:y+(wl-1)
if (wind(r,c)==0)&&(wind(r,c+1)==v)
T0v=T0v+1;
end
end
c=y+wl;
for r=x-wl:x+(wl-1)
if (wind(r,c)==0)&&(wind(r+1,c)==v)
T0v=T0v+1;
end
end
r=x+wl;
for c=y+wl:-1:y-(wl-1)
if (wind(r,c)==0)&&(wind(r,c-1)==v)
T0v=T0v+1;
end
end
c=y-wl;
for r=x+wl:-1:x-(wl-1)
if (wind(r,c)==0)&&(wind(r-1,c)==v)
T0v=T0v+1;
end
end
T1=T0v==1;
T=T & T1;
end
if T~=1
minutiae_valid(i,:)=[0 0 0 0];
combined(x,y,:)=[255,255,255];
end
end
end
minu_count=1;
minutiae_valid_final(minu_count, :) = [0,0,0,0]; %to make x, y, CN, theta value of minutiae not in border region.
for i=1:totminu_I
CN = minutiae_valid(i,3);
if (CN~=0)
minutiae_valid_final(minu_count, :) = minutiae_valid(i,:);
minu_count = minu_count + 1;
end
end
% totminu_I= minu_count-1;
minutiae_valid = minutiae_valid_final;
% figure, subimage(combined);
At lines 212 and 213 you create nested loops that loop over nine elements of wind looking for elements that match some sort of condition. When you find one, you change the center of your nine element block.
The problem arises when the center of your block is on an edge of wind. When you look over the nine elements centered at an edge you must not access indices in the same way, otherwise you get the error as in above. To illustrate a simple case of this:
a = rand(4,4);
for p=2:4
for q = 1:3
tmp += a(p,q)
end
end
sums the 9 elements of a (a square array) centered at (3,2).
a = rand(4,4);
for p=3:5
for q = 1:3
tmp += a(p,q)
end
end
will generate the same error that you have when it tries to access a(5,1) because this element of the array does not exist. The correct way to fix this depends on what you're trying to do, but that's most definitely beyond the scope of your question, and our ability to figure out.
I hope this helps.
Echoing some of the comments, I'd like to offer you a piece of advice. In the future, you're much more likely to get on taget, quality answers if you can boil your question down to the shortest possible test case. This is also a valuable step in debugging your own code. Figuring out how to construct a test case will often make the answer to your problem apparent. When it's not apparent, Stackoverflow is an excellent resource.