打印几乎每个输入的 MATLAB 函数

以下 MATLAB 函数可用于使用 fprintf 打印每个输入,无论其类型如何。由于它不使用 mat2str(),因此也可以在 MATLAB Coder 代码生成中使用。

printit.m
function printit(inputValue)
    if isnumeric(inputValue) || islogical(inputValue)
        % Print numeric/logical arrays element-wise
        fprintf('Input: %g \n', inputValue(:));
    elseif ischar(inputValue)
        fprintf('Input: %s\n', inputValue);
    elseif isstring(inputValue)
        fprintf('Input: %s\n', char(inputValue));
    elseif iscell(inputValue)
        disp('Input (cell):');
        disp(inputValue);
    elseif isstruct(inputValue)
        disp('Input (struct):');
        disp(inputValue);
    else
        disp('Input (unknown type):');
        disp(inputValue);
    end
end

Check out similar posts by category: Matlab/Simulink