-->

Subscript indices must either be real positive int

2019-01-29 00:58发布

问题:

for control=1:7
    name=strcat('tau: ', num2str(TD(control)),'   PLD: ', num2str(PLD(control)));
    fprintf('Control: %i/7\n', control)
    Maps(control) = struct('GradOFF', [], 'GradON', []);
    for lin=1:size(data(subject).perf_w_off,1)
        fprintf('Lin: %i/64\n', lin);
        for col=1:size(data(subject).perf_w_off,2)
            [x1, fval, exitflag, output] = fminunc(...)
            Maps(control).GradOFF(lin,col) = abs(x1(2));
            [x2, fval, exitflag, output] = fminunc(...)
            Maps(control).GradON(lin,col) = abs(x2(2));
        end
    end

    min1 = min(min(Maps(control).GradOFF));
    min2 = min(min(Maps(control).GradON));
    min = min([min1, min2]);
    max1 = max(max(Maps(control).GradOFF));
    max2 = max(max(Maps(control).GradON));
    max = max([max1, max2]);

    figure(map1);

    subplot(2,7,control)
    image1 = mat2gray(Maps(control).GradOFF,[min, max]);
    imshow(image1,[]);
    title(strcat(name, ' Grad Off'))

    subplot(2,7, control+7)
    image2 = mat2gray(Maps(control).GradON,[min, max]);
    imshow(image2,[]);
    title(strcat(name, ' Grad On'))
end

Nothing seems to go wrong with this code. The first loop (the one over control) is doing fine. However when going through the loop again (control = 2), then an error message appears when doing min1 = min(min(Maps(control).GradOFF)). It says:

Subscript indices must either be real positive integers or logicals.

but when I'm doing mean(mean(Maps(control).GradOFF)) it is working. Could someone tell me what could go possibly wrong with min that won't with mean?

回答1:

You are defining a variable min in this line:

min = min([min1, min2]);

Once you have done that, min1 = min(min(Maps(control).GradOFF)); is being interpreted as a reference to the variable min, not the function, hence the error on the second time around. Rename that variable so it's not got the same name as the function. The same goes for max = max([max1, max2]); which will give you the same problem if you don't correct it.