% --- Executes on button press in voltage.
function voltage_Callback(hObject, eventdata, handles)
global shape volt_press mapping electrode
volt_press=volt_press+1;

[disc,page]=size(shape);
if volt_press==1
%each page of 'mapping' will correspond to shape.id 
%which contain common nodes
    for i=1:page
        temp1=shape(i).elem;
        k=1;
        for j=1:page
            temp2=shape(j).elem;
            [m,n]=size(intersect(temp1,temp2,'rows'));
            if m~=0
                mapping(i).ch(k)=j;
                k=k+1;
            end
        end
    end
    
    check=page;
    i=1;
    while i<=check
        array1=mapping(i).ch;
        j=1;
        while j<=check
            if j==i
                j=j+1;
            else
                array2=mapping(j).ch;
                [m,n]=size(intersect(array1,array2));
                if n~=0
                    array1=union(array1,array2);
                    mapping(j)=[];
                    j=j-1;
                    check=check-1;
                end
                j=j+1;
            end
        end
        mapping(i).ch=array1;
        i=i+1;
    end 
end
%at the end of this loop, true 'mapping' has been formed

[disc,page]=size(mapping);
for i=1:page
    temp=[0 0];
    [disc,aw]=size(mapping(i).ch);
    for j=1:aw
        tr=mapping(i).ch(1,j);
        sh=shape(tr).elem;
        temp=union(temp,sh,'rows');
    end
    temp(1,:)=[];
    electrode(i).elem=temp;
end
%at the end of this loop, structure 'electrode' is fully formed

[nx,ny]=ginput(1);
prompt1={'Enter the voltage of the chosen electrode'};
V=inputdlg(prompt1);
v=str2double(V{1,1});

flag=0;
xvolt=round(nx);
yvolt=round(ny);
[disc,page]=size(electrode);
if volt_press==1
    for i=1:page
        electrode(i).voltage='not';  %declaring voltages of all electrodes as 'not entered'
    end
end

for i=1:page
    temp=electrode(i).elem;
    [mtemp,ntemp]=size(temp);
    for j=1:mtemp
        if (temp(j,1)==xvolt)&&(temp(j,2)==yvolt)
            flag=1;
            number=i;
            break
        end
    end
    if flag==1
        break
    end
end

if flag==0
    msgbox('Error: No electrode at selected point')
else
    if electrode(number).voltage=='not'
        electrode(number).voltage=v;  
        array=mapping(number).ch;
        makered(array)
        x=xvolt;
        y=yvolt;
        [xc,yc]=findmid(x,y);
        text(xc,yc,V,'FontSize',12,'FontWeight','bold');
    else
        msgbox('Voltage has already been entered')
    end
end


function makered(array)
%conveys that voltage has been entered by 
%converting the electrode to red color
global shape
[m1,n1]=size(array);
for i=1:n1
    temp=shape(array(i)).elem;
    x1=min(temp(:,1));
    x2=max(temp(:,1));
    w=x2-x1;
    y1=min(temp(:,2));
    y2=max(temp(:,2));
    h=y2-y1;
    handle=findobj(gca,'type','rectangle','Position',[x1,y1,w,h]);
    set(handle,'FaceColor','r','EdgeColor','r');
    handle=findobj(gca,'type','rectangle','Position',[x1,y1,w,h],'Curvature',[1 1]);
    set(handle,'FaceColor','r','EdgeColor','r');
    hold on
    gridplot()
end
return

