function [data,string] = pdeptdata(prop,type,p,~,t,c,uv,appl,params,hndls,time)
%PDEPTDATA Compute data and labels for pdeModeler plots.

%       Copyright 1994-2023 The MathWorks, Inc.

arguments
    prop % index of "Property" option in Plot Selection dialog box
    type % index of "Plot type" in Plot Selection dialog box
    p
    ~
    t
    c
    uv
    appl % index of "Application" option in PDEModeler tool
    params
    hndls
    time = []
end

% User can specify custom formulae for color, arrow, deformation, and
% height in pdeModeler. Variables may be used that appear to be unused.
%#ok<*NASGU>
%#ok<*ASGLU>

syst        = ismember(appl,2:4);   % Generic System, Plane Strain, Plane Stress
nnode       = size(p,2);
userflag    = false;
u           = uv(1:nnode);
if length(uv)==2*nnode              % uv is either u or [u;v]
    v       = uv(nnode+1:2*nnode);
end

% Default outputs
data        = [];
string      = '';

% String references
type12prop1 = {'u','u','u','u','V','A','E','V','T','c'};
type12prop2 = {'abs(grad(u))','v','v','v','abs(E)', ...
    'abs(B)','abs(B)','abs(E)','abs(grad(T))','abs(grad(c))'};
type1prop3  = {'abs(c*grad(u))','abs(u,v)','abs(u,v)','abs(u,v)','abs(D)', ...
    'abs(H)','abs(H)','abs(J)','abs(q)','abs(q)'};
type2prop3  = {'abs(grad(u))','abs(u,v)','abs(u,v)','abs(u,v)','abs(D)', ...
    'abs(H)','abs(H)','abs(J)','abs(q)','abs(q)'};
type12prop4 = {'','','ux','ux','','','i','','',''};
type12prop5 = {'','','uy','uy','','','Q','','',''};
type12prop6 = {'','','vx','vx','','','','','',''};
type12props = {'','','','','','','vy','exx','eyy','exy', ... % 1-10
    'sxx','syy','sxy','e1','e2','s1','s2','von Mises',''};   % 11-19
type12aps   = {'','','ps','pn'};
type34prop1 = {'-grad(u)','(u,v)','(u,v)','(u,v)','E', ...
    'B','B','E','-grad(T)','-grad(c)'};
type34prop2 = {'-c*grad(u)','','','','E','H','H','J','q','q'};


if type==1 % xydata
    if prop==1 % u
        string = type12prop1{appl};
        data = u;
    elseif prop==2
        string = type12prop2{appl};
        if syst % v
            data    = v;
        elseif appl==7 % AC Power Electromagnetics case: abs(B)=abs(j/omega*curl(E))
            omega   = pdetxpd(p,t,uv,[],deblank(params(1,:)));
            [ux,uy] = pdegrad(p,t,uv);
            data    = (1./omega).*sqrt(ux.^2+uy.^2);
        else % abs(grad(u))
            [ux,uy] = pdegrad(p,t,uv);
            data    = sqrt(ux.^2+uy.^2);
        end
    elseif prop==3
        string = type1prop3{appl};
        if syst % abs(u,v)
            data    = sqrt(u.^2+v.^2);
        elseif appl==7 % AC Power Electromagnetics case: abs(H)=abs(1/mu*j/omega*curl(E))
            omega   = pdetxpd(p,t,uv,[],deblank(params(1,:)));
            mu      = pdetxpd(p,t,uv,[],deblank(params(2,:)));
            [ux,uy] = pdegrad(p,t,uv);
            data    = (1./mu).*(1./omega).*sqrt(ux.^2+uy.^2);
        else % abs(c*grad(u))
            [ux,uy] = pdecgrad(p,t,c,uv,time);
            data    = sqrt(ux.^2+uy.^2);
        end
    elseif prop==4 % structural mechanics:
        string = type12prop4{appl};
        if (appl==3 || appl==4)
            ap      = type12aps{appl};
            data    = pdesmech(p,t,c,uv,'tensor',string,'application',ap,'nu',params(2,:));
        elseif appl==7 % AC Power Electromagnetics case: i=sigma*E
            sigma   = pdetxpd(p,t,uv,[],deblank(params(3,:)));
            if length(sigma)==size(t,2)
                sigma = pdeprtni(p,t,sigma);
            end
            data    = sigma.*uv;
        else
            userflag = true;
        end
    elseif prop==5 % structural mechanics:
        string = type12prop5{appl};
        if (appl==3 || appl==4)
            ap      = type12aps{appl};
            data    = pdesmech(p,t,c,uv,'tensor',string,'application',ap,'nu',params(2,:));
        elseif appl==7 % AC Power Electromagnetics case: Q=E^2/sigma
            sigma   = pdetxpd(p,t,uv,[],deblank(params(3,:)));
            if length(sigma)==size(t,2)
                sigma = pdeprtni(p,t,sigma);
            end
            data    = uv.*uv.*sigma;
        end
    elseif prop==6 % structural mechanics:
        string = type12prop6{appl};
        if (appl==3 || appl==4)
            ap      = type12aps{appl};
            data    = pdesmech(p,t,c,uv,'tensor',string,'application',ap,'nu',params(2,:));
        elseif appl==7 % AC Power Electromagnetics case: user entry
            userflag = true;
        end
    elseif prop>6 % structural mechanics:
        string = type12props{prop};
        if prop==19 % user entry:
            userflag = true;
        else
            if (appl==3 || appl==4)
                ap = type12aps{appl};
            end
            data = pdesmech(p,t,c,uv,'tensor',string,'application',ap,'nu',params(2,:));
        end
    end

    if userflag
        if syst
            [uvx,uvy] = pdegrad(p,t,uv);
            ux = pdeprtni(p,t,uvx(1,:));
            vx = pdeprtni(p,t,uvx(2,:));
            uy = pdeprtni(p,t,uvy(1,:));
            vy = pdeprtni(p,t,uvy(2,:));
            [cuvx,cuvy] = pdecgrad(p,t,c,uv,time);
            cux = pdeprtni(p,t,cuvx(1,:));
            cvx = pdeprtni(p,t,cuvx(2,:));
            cuy = pdeprtni(p,t,cuvy(1,:));
            cvy = pdeprtni(p,t,cuvy(2,:));
        else
            [ux,uy] = pdegrad(p,t,uv);
            ux = pdeprtni(p,t,ux);
            uy = pdeprtni(p,t,uy);
            [cux,cuy] = pdecgrad(p,t,c,uv,time);
            cux = pdeprtni(p,t,cux);
            cuy = pdeprtni(p,t,cuy);
        end
        x = p(1,:)';
        y = p(2,:)';
        try
            string  = deblank(get(hndls(12),'String'));
            ttemp   = t;
            t       = time;
            data    = eval(string);
            t       = ttemp;
        catch ME
            if isequal(ME.identifier,'MATLAB:m_incomplete_statement')
                pdeModeler('error',' No user entry supplied.')
            else
                pdeModeler('error',sprintf(' Unable to evaluate ''%s''.',string))
            end
            data    = NaN;
            string  = '';
        end
    end

elseif type==2 % zdata
    userflag = 0;
    if prop==1 % u
        string = type12prop1{appl};
        data = u;
    elseif prop==2
        string = type12prop2{appl};
        if syst % v
            data    = v;
        elseif appl==7 % AC Power Electromagnetics case: abs(B)=abs(j/omega*curl(E))
            omega   = pdetxpd(p,t,uv,[],deblank(params(1,:)));
            [ux,uy] = pdegrad(p,t,uv);
            data    = (1./omega).*sqrt(ux.^2+uy.^2);
        else % abs(grad(u))
            [ux,uy] = pdegrad(p,t,uv);
            data    = sqrt(ux.^2+uy.^2);
        end
    elseif prop==3
        string = type2prop3{appl};
        if syst % abs(u,v)
            data    = sqrt(u.^2+v.^2);
        elseif appl==7 % AC Power Electromagnetics case: abs(H)=abs(1/mu*j/omega*curl(E))
            omega   = pdetxpd(p,t,uv,[],deblank(params(1,:)));
            mu      = pdetxpd(p,t,uv,[],deblank(params(2,:)));
            [ux,uy] = pdegrad(p,t,uv);
            data    = (1./mu).*(1./omega).*sqrt(ux.^2+uy.^2);
        else % abs(c*grad(u))
            [ux,uy] = pdecgrad(p,t,c,uv,time);
            data    = sqrt(ux.^2+uy.^2);
        end
    elseif prop==4 % structural mechanics:
        string = type12prop4{appl};
        if (appl==3 || appl==4)
            ap      = type12aps{appl};
            data    = pdesmech(p,t,c,uv,'tensor',string,'application',ap,'nu',params(2,:));
        elseif appl==7 % AC Power Electromagnetics case: i=sigma*E
            sigma   = pdetxpd(p,t,uv,[],deblank(params(3,:)));
            if length(sigma)==size(t,2)
                sigma = pdeprtni(p,t,sigma);
            end
            data    = sigma.*uv;
        else
            userflag = true;
        end
    elseif prop==5 % structural mechanics:
        string = type12prop5{appl};
        if (appl==3 || appl==4)
            ap      = type12aps{appl};
            data    = pdesmech(p,t,c,uv,'tensor',string,'application',ap,'nu',params(2,:));
        elseif appl==7 % AC Power Electromagnetics case: Q=E^2/sigma
            sigma   = pdetxpd(p,t,uv,[],deblank(params(3,:)));
            if length(sigma)==size(t,2)
                sigma = pdeprtni(p,t,sigma);
            end
            data    = uv.*uv./sigma;
        end
    elseif prop==6 % structural mechanics:
        string = type12prop6{appl};
        if (appl==3 || appl==4)
            ap      = type12aps{appl};
            data    = pdesmech(p,t,c,uv,'tensor',string,'application',ap,'nu',params(2,:));
        elseif appl==7 % AC Power Electromagnetics case: user entry
            userflag = true;
        end
    elseif prop>6 % structural mechanics:
        string = type12props{prop};
        if prop==19 % user entry:
            userflag = true;
        else
            ap = type12aps{appl};
            data = pdesmech(p,t,c,uv,'tensor',string,'application',ap,'nu',params(2,:));
        end
    end

    if userflag
        if syst
            [uvx,uvy] = pdegrad(p,t,uv);
            ux = pdeprtni(p,t,uvx(1,:));
            vx = pdeprtni(p,t,uvx(2,:));
            uy = pdeprtni(p,t,uvy(1,:));
            vy = pdeprtni(p,t,uvy(2,:));
            [cuvx,cuvy] = pdecgrad(p,t,c,uv,time);
            cux = pdeprtni(p,t,cuvx(1,:));
            cvx = pdeprtni(p,t,cuvx(2,:));
            cuy = pdeprtni(p,t,cuvy(1,:));
            cvy = pdeprtni(p,t,cuvy(2,:));
        else
            [ux,uy] = pdegrad(p,t,uv);
            ux = pdeprtni(p,t,ux);
            uy = pdeprtni(p,t,uy);
            [cux,cuy] = pdecgrad(p,t,c,uv,time);
            cux = pdeprtni(p,t,cux);
            cuy = pdeprtni(p,t,cuy);
        end
        x = p(1,:)';
        y = p(2,:)';
        try
            string  = deblank(get(hndls(15),'String'));
            ttemp   = t;
            t       = time;
            data    = eval(string);
            t       = ttemp;
        catch ME
            if isequal(ME.identifier,'MATLAB:m_incomplete_statement')
                pdeModeler('error',' No user entry supplied.')
            else
                pdeModeler('error',sprintf(' Unable to evaluate ''%s''.',string))
            end
            data = NaN;
            string = '';
        end
    end

elseif type==3 % flowdata:
    if prop==1
        string = type34prop1{appl};
        if syst % (u,v)
            data    = pdeintrp(p,t,uv);
        elseif appl==6 % Magnetostatics case, B=curl(A)
            [ux,uy] = pdegrad(p,t,uv);
            data    = [uy; -ux];
        elseif appl==7 % AC Power Electromagnetics case, B=j/omega*curl(E)
            omega   = pdetxpd(p,t,uv,[],deblank(params(1,:)));
            [ux,uy] = pdegrad(p,t,uv);
            data    = [(1./omega).*uy; -(1./omega).*ux];
        else % grad u
            [ux,uy] = pdegrad(p,t,uv);
            data    = [-ux; -uy];
        end
    elseif prop==2 && ~syst
        string      = type34prop2{appl};
        if appl==6 % Magnetostatics case, H=(1/mu)*curl(A)
            [ux,uy] = pdecgrad(p,t,c,uv,time);
            data    = [uy; -ux];
        elseif appl==7 % AC Power Electromagnetics case, H=1/mu*j/omega*curl(E)
            omega   = pdetxpd(p,t,uv,[],deblank(params(1,:)));
            mu      = pdetxpd(p,t,uv,[],deblank(params(2,:)));
            [ux,uy] = pdegrad(p,t,uv);
            data    = [(1./mu).*(1./omega).*uy; -(1./mu).*(1./omega).*ux];
        else % c*grad(u) (scalar case only)
            [ux,uy] = pdecgrad(p,t,c,uv,time);
            data    = [-ux; -uy];
        end
    elseif prop==3 || (prop==2 && syst) % user entry
        if syst
            [uvx,uvy] = pdegrad(p,t,uv);
            ux = uvx(1,:);
            vx = uvx(2,:);
            uy = uvy(1,:);
            vy = uvy(2,:);
            [cuvx,cuvy] = pdecgrad(p,t,c,uv,time);
            cux = cuvx(1,:);
            cvx = cuvx(2,:);
            cuy = cuvy(1,:);
            cvy = cuvy(2,:);
        else
            [ux,uy] = pdegrad(p,t,uv);
            [cux,cuy] = pdecgrad(p,t,c,uv,time);
        end
        x = pdeintrp(p,t,p(1,:)');
        y = pdeintrp(p,t,p(2,:)');
        try
            string  = deblank(get(hndls(13),'String'));
            ttemp   = t;
            t       = time;
            data    = eval(string);
            t       = ttemp;
            [m,n]   = size(data);
            if m~=2 || n~=size(t,2)
                if m*n~=2*nnode
                    pdeModeler('error',' Incorrect data format.')
                    data = NaN;
                    string = '';
                    return
                end
                data = data(:);
                data = pdeintrp(p,t,data);
            end
        catch ME
            if isequal(ME.identifier,'MATLAB:m_incomplete_statement')
                pdeModeler('error',' No user entry supplied.')
            else
                pdeModeler('error',sprintf(' Unable to evaluate ''%s''.',string))
            end
            data = NaN;
            string = '';
        end
    end

elseif type==4 % deformdata:
    string = type34prop1{appl};
    if prop==1
        if syst % (u,v)
            data    = uv;
        elseif appl==6 % Magnetostatics case, B=curl(A)
            [ux,uy] = pdegrad(p,t,uv);
            data    = pdeprtni(p,t,[uy;-ux]);
        elseif appl==7 % AC Power Electromagnetics case, B=j/omega*curl(E)
            omega   = pdetxpd(p,t,uv,[],deblank(params(1,:)));
            [ux,uy] = pdegrad(p,t,uv);
            data    = pdeprtni(p,t,[(1./omega).*uy; -(1./omega).*ux]);
        else % grad u
            [ux,uy] = pdegrad(p,t,uv);
            data    = pdeprtni(p,t,[-ux; -uy]);
        end
    elseif prop==2 && ~syst
        string      = type34prop2{appl};
        if appl==6 % Magnetostatics case, H=(1/mu)*curl(A)
            [ux,uy] = pdecgrad(p,t,c,uv,time);
            data    = pdeprtni(p,t,[uy; -ux]);
        elseif appl==7 % AC Power Electromagnetics case, H=1/mu*j/omega*curl(E)
            omega   = pdetxpd(p,t,uv,[],deblank(params(1,:)));
            mu      = pdetxpd(p,t,uv,[],deblank(params(2,:)));
            [ux,uy] = pdegrad(p,t,uv);
            data    = pdeprtni(p,t,[(1./mu).*(1./omega).*uy; -(1./mu).*(1./omega).*ux]);
        else % c*grad(u) (scalar case only)
            [ux,uy] = pdecgrad(p,t,c,uv,time);
            data    = pdeprtni(p,t,[-ux; -uy]);
        end
    elseif prop==3 || (prop==2 && syst) % user entry
        if syst
            [uvx,uvy] = pdegrad(p,t,uv);
            ux = uvx(1,:);
            vx = uvx(2,:);
            uy = uvy(1,:);
            vy = uvy(2,:);
            [cuvx,cuvy] = pdecgrad(p,t,c,uv,time);
            cux = cuvx(1,:);
            cvx = cuvx(2,:);
            cuy = cuvy(1,:);
            cvy = cuvy(2,:);
        else
            [ux,uy] = pdegrad(p,t,uv);
            [cux,cuy] = pdecgrad(p,t,c,uv,time);
        end
        x = pdeintrp(p,t,p(1,:)');
        y = pdeintrp(p,t,p(2,:)');
        try
            string  = deblank(get(hndls(14),'String'));
            ttemp   = t;
            t       = time;
            data    = eval(string);
            t       = ttemp;
            [m,n]   = size(data);
            if m~=2 || n~=size(t,2)
                if m*n~=2*nnode
                    pdeModeler('error',' Incorrect data format.')
                    data = NaN;
                    string = '';
                    return
                end
                data = data(:);
                data = pdeintrp(p,t,data);
            end
            data = pdeprtni(p,t,data);
        catch ME
            if isequal(ME.identifier,'MATLAB:m_incomplete_statement')
                pdeModeler('error',' No user entry supplied.')
            else
                pdeModeler('error',sprintf(' Unable to evaluate ''%s''.',string))
            end
            data = NaN;
            string = '';
        end
    end
    data = real(data);
end