How to select a specific .m function when two exist?

后端 未结 4 824
野的像风
野的像风 2021-01-17 19:13

First, here the way i\'m calling the function :

eval([functionName \'(\'\'stringArg\'\')\']); % functionName = \'someStringForTheFunctionName\'
4条回答
  •  不要未来只要你来
    2021-01-17 19:29

    OK, so a messy answer, but it should do it. My test function was 'echo'

    funcstr='echo'; % string representation of function
    
    Fs=which('-all',funcstr);
    for v=1:length(Fs)
        if (strcmp(Fs{v}(end-1:end),'.m')) % Don''t move built-ins, they will be shadowed anyway
            movefile(Fs{v},[Fs{v} '_BK']);
        end
    end
    for v=1:length(Fs)
        if (strcmp(Fs{v}(end-1:end),'.m')) 
            movefile([Fs{v} '_BK'],Fs{v});
        end
        try
            eval([funcstr '(''stringArg'')']);
            break;
        catch
            if (strcmp(Fs{v}(end-1:end),'.m'))
                movefile(Fs{v},[Fs{v} '_BK']);
            end
        end
    end
    for w=1:v
        if (strcmp(Fs{v}(end-1:end),'.m'))
           movefile([Fs{v} '_BK'],Fs{v});
        end
    end
    

提交回复
热议问题