function z = CheckZ(z,m,desc,option)
%CHECKZ Check the impedance value and reshape or scalar expand if needed
%
%   % EXAMPLE: Check Z0 and ZL
%   z0 = 50;
%   zl = 50;
%   m = 10;
%   z0 = CheckZ(z0,m,'Z0');   
%   zl = CheckZ(zl,m,'ZL');  
% 
%   See also CHECKNETWORKDATA

%   Copyright 2007-2015 The MathWorks, Inc.

narginchk(2,4)

% Get and check the option
if nargin < 4
    option = 1;
    if nargin < 3
        desc = 'Z';
    end
end

z = squeeze(z);
% Must be numeric
if ~isnumeric(z) 
    error(message('rflib:CheckZ:NotNumeric',desc))
end
% NaN is not allowed
if any(isnan(z)) 
    error(message('rflib:CheckZ:IsNaN',desc))
end

% Must be a scalar or vector
if ~isvector(z) || ((numel(z) ~= 1) && (numel(z) ~= m))
    switch option
        case 0
            validateattributes(z,{'numeric'},{'vector','numel',m},'',desc)
        case 1
            error(message('rflib:CheckZ:ZWrongInput_NetParam',desc))
        case 2
            error(message('rflib:CheckZ:ZWrongInput_SimFreq',desc))
        case 3
            error(message('rflib:CheckZ:ZWrongInput_Gamma',desc))
        case 4
            error(message('rflib:CheckZ:ZWrongInput_Z',desc))
        otherwise
            validateattributes(z,{'numeric'},{'vector','numel',m},'',desc)
    end
end

if strcmpi(desc,'Z0') && (any(z == 0) || any(isinf(z)))
    error(message('rflib:CheckZ:Z0WrongInput'))
end

% Reshape
if numel(z) == m
    z = reshape(z,[1,1,m]);
else
    z = z*ones(1,1,m);
end