Is there a Matlab function to convert any data structure to a string?

前端 未结 4 1287
情话喂你
情话喂你 2020-12-10 16:26

I\'m looking for a function in Matlab to use for error messages, like so:

error([\'Invalid value for someVariable: \' wantedFunction(someVariable)]);
         


        
4条回答
  •  甜味超标
    2020-12-10 17:06

    No, there is no such function. I ran into similar problems, so here is a very rudimentary function that I use. Do realize that it is not complete. For example, it does not output fields of a structure in a meaningful way, but that can easily be added. You can treat it as a base implementation and fit it to your needs.

    function ret = all2str(param)
    if isempty(param)
        if iscell(param)
            ret = '(empty cell)';
        elseif isstruct(param);
            ret = '(empty struct)';
        else
            ret = '(empty)';
        end
        return;
    end
    
    if ischar(param)
        ret = param;
        return;
    end
    
    if isnumeric(param)
        ret = num2str(param);
        return;
    end
    
    if iscell(param)
        ret = all2str(param{1});
        for i=2:numel(param)
            ret = [ret ', ' all2str(param{i})];
        end
        return;
    end
    
    if isstruct(param)
        ret = '(structure)';
        return;
    end
    end
    

提交回复
热议问题