% Draw the result of a finite difference demo for electrical impedance
% tomography. This routine is for 1D demos.
%
% Inputs:
% potvec       vector of electric voltage potentials, 1xN vector
% fignum       figure window number
% bdata_l      voltage potential at the left end of the 1D bar
% bdata_r      voltage potential at the right end of the 1D bar
% plot_x       value 1 means plotting variable names instead of values
% show_inside  if zero, then plot voltage and/or current only at boundary
% font_size    [optional] font size
% digitn       [optional] how many digits to show in the potential values
%
% Samuli Siltanen March 2025

function EIT_FD_demo1D_text_plot(potvec,fignum,bdata_l,bdata_r,plot_x,show_inside,font_size,digitn)


% Record the size of the FD solution vector
[row,col] = size(potvec);
if row>1
    error('Potential vector should be horizontal (and a vector, not matrix)')
end
N = col; % Number of voltage potentials in the solution vector

% Set geometrical parameters
geometry_set;
if nargin>6 % If user defined fontsize in the optional argument
    fsize = font_size;
end
if nargin>7 % If user defined digit accuracy in the optional argument
    formatstr = ['%0.',num2str(digitn),'f'];
else 
    formatstr = '%0.1f';
end


% Record minimum and maximum voltages
vmin = min([min(potvec),bdata_l,bdata_r]);
vmax = max([max(potvec),bdata_l,bdata_r]);

% Create plot window
figure(fignum)
clf

% Plot grid
p1 = plot([0 N],[0 0],'k','linewidth',lwidth_grid);
set(p1,'color',color_gridline)
hold on
p2 = plot([0 N],[Dx Dx],'k','linewidth',lwidth_grid);
set(p2,'color',color_gridline)
for jjj = 1:N+1
    p3 = plot((jjj-1)*[Dx Dx],[0 Dx],'k','linewidth',lwidth_grid);
    set(p3,'color',color_gridline)
end

% If the user wants, show pixel names
if plot_x
    % Left boundary
    text((-.7)*Dx,0.5,'v_1','fontsize',fsize)
    % Right boundary
    text((N+.3)*Dx,0.5,'v_2','fontsize',fsize)
    % Middle pixels
    for jjj = 1:N
        pix_text = ['u_{',num2str(jjj),'}'];
        text((jjj-.7)*Dx,0.5,pix_text,'fontsize',fsize)
    end
else % If not variable names, then voltage potential values
    % Voltage at left electrode
    pix_text = num2str(bdata_l,formatstr);
    text(-Dx,0.5,pix_text,'fontsize',fsize)
    % Voltage at right electrode
    pix_text = num2str(bdata_r,formatstr);
    text((N+.2)*Dx,0.5,pix_text,'fontsize',fsize)
    if show_inside % Voltages in the grid
        for ccc = 1:N
            pix_text = num2str(potvec(ccc),formatstr);
            text((ccc-.9)*Dx,.5*Dx,pix_text,'fontsize',fsize)
        end
    end
end



% Axis settings
axis([-2*Dx (N+2)*Dx -2*Dx 3*Dx])
axis equal
axis off


