function abspath = rel2abs(relpath)

% 簡易的なエラー処理
if ~(ischar(relpath) || iscell(relpath) || isstring(relpath))
    warning('引数はchar型，cell型，string型のいずれかにしてください．')
    abspath = relpath;
    return
end

% relpathが要素数1のstring型の場合，char型に変換
num = numel(relpath);
outString = 0;
if isstring(relpath) && num == 1
    relpath = char(relpath);
    outString = 1;
end

% relpathが要素数2以上のstring配列の場合，各要素ごとに処理
if isstring(relpath) && num >= 2
    abspath = string;
    for ii = 1:num
        abspath(ii) = rel2abs(relpath(ii));
    end
    abspath = reshape(abspath, size(relpath));
    return
end

% relpathがcell配列の場合，各要素ごとに処理
if iscell(relpath)
    abspath = {};
    for ii = 1:num
        abspath{ii} = rel2abs(relpath{ii});
    end
    abspath = reshape(abspath, size(relpath));
    return
end

% ファイル名のみの場合，現在のパスを追加
[pathname, filename, ext] = fileparts(relpath);
if isempty(pathname)
    relpath = fullfile('.', [filename, ext]);
end

try     % 絶対パスに変換
    [~, vals] = fileattrib(relpath);
    abspath = vals.Name;
    
catch   % エラーが生じた場合，1段上の階層で同じ処理を試みる
        % エラーが出るのは主に，引数として指定したパスが存在しない場合
    
    folders = strsplit(relpath, {'/', '¥', '\\'});
    
    % Macのパスに対応
    if isempty(folders{1})
        folders{2} = ['/', folders{2}];
        folders = folders(2:end);
    end
    
    if length(folders) == 1     % 最上階層にきたら処理を終了
        abspath = folders{1};
    elseif length(folders) >= 2 % 最上階層でなければ1段上の階層へ
        tmp = folders{1};
        for ii = 2:(length(folders) - 1)
            tmp = fullfile(tmp, folders{ii});
        end
        tmp2 = rel2abs(tmp);    % 1段上の階層でrel2abs
        abspath = fullfile(tmp2, folders{end});
    end
end % try&catch

% 引数がstring型なら戻り値もstring型に変換
if outString
    abspath = string(abspath);
end

end % function