如何使用strcmp()将函数的返回值与循环中的函数调用进行比较?

时间:2013-03-12 00:34:52

标签: matlab

我遇到的问题是我试图将fireShot()命令的结果与结果'Splash'进行比较。接下来我需要计算总真值(或每次fireShot()='Splash')。但是,每当我尝试使用while循环或if循环时,我最终都会得到一个常数(在相应的图上与相应的随机数或我之前的函数不匹配)。我确信这种格式不是很有吸引力,可以使用工作,但我真正的问题在于,我相信,strcmp()的值的统计。

无论如何这是我的代码:

function [ output_args ] = fireShots(shotstofire, fieldSize, pondRadius)
%// This function will fire multiple shots and count how many of them     
%// land in the pond.

    %// Counts down total number of shots and subtracts 1 until
    %// total is equal to '0'    
    while shotstofire > 0
        shotstofire;
        shotstofire = shotstofire - 1;

        %// Calls fireShot function
        Splash = fireShot(fieldSize,pondRadius);
        Land = strcmp(Splash,'Splash');
    end

    %// If the ball hits inside the pond
    %// Land (total tally) gets +1
    if (Land)
        Land = Land + 1;
    else

    end        
end

我知道我的代码可能已关闭,但此功能的所有功能都可以正常工作。

1 个答案:

答案 0 :(得分:0)

然后你的问题实际上只是一个逻辑错误; strcmp检查很好,你只需要在你的while循环中移动你的Land计数逻辑来获得'Splash'es的总镜头数。实际上,当您在while循环之外执行检查和增量时,根据最后Land的结果,您最多会获得strcmp变量的+1。

function [ output_args ] = fireShots(shotstofire, fieldSize, pondRadius)
    %// This function will fire multiple shots and count how many of them     
    %// land in the pond.

    %// Counts down total number of shots and subtracts 1 until
    %// total is equal to '0'    
    while shotstofire > 0
        shotstofire;
        shotstofire = shotstofire - 1;

        %// Calls fireShot function
        Splash = fireShot(fieldSize,pondRadius);

        %// If the ball hits inside the pond
        %// Land (total tally) gets +1
        if (strcmp(Splash,'Splash'))
            Land = Land + 1;
        end
    end      
end