Matlab error "Integer operands are required for colon operator when used as index"

StackOverflow https://stackoverflow.com/questions/20829562

  •  22-09-2022
  •  | 
  •  

문제

I'm using a slider to increment values of f. It gives me the action I desire on my GUI -- but my command window shows this warning:

Warning: Integer operands are required for colon operator when used as index

In fact, if I output the value of f I see that it's not a round integer.

I've tried fix, round, floor etc to no avail.

Why am I still getting this warning?

f      = get(handles.slider_frames, 'Value');
f      = round(f);
window = 4;

while f > 0
    x = output(:, f:f + window - 1);  % <---- warning points to this line
    x = mean(x,2);

    %... code continues

end
도움이 되었습니까?

해결책

Make sure that round(f) gives you value greater 0.
If round(f) == 0, you'll end up with indexing starting from zero:

> f=0; f:f + 4 - 1
ans =
 0     1     2     3

> f=1; f:f + 4 - 1
ans =
 1     2     3     4

Added: To avoid this you should play with Min, Max, and SliderStep properties of your slider so Value will return you integer values (or at least values very close to integers witj minimal round-off errors).
For example, if you want

Slider.Min = 1;
Slider.Max = 79;

Then you should put

Slider.SliderStep = [1 10] / ( Slider.Max - Slider.Min )

In this case click on the slider button with arrow will increase slider value by 1, but when you click the slider trough, slider value will increase by 10. See documentation on the SliderStep property of uicocntrol for more explanations.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top