MATLAB Answers

slider for march chart

3 views (last 30 days)
Lev Mihailov
Lev Mihailov on 15 May 2020
Answered: Geoff Hayes on 15 May 2020
Hello! Help adjust the slider for the chart of the marshout (path) of the machine
function SliderValueChanged(app, event)
app.Slider = app.Slider.Value;
end
function GPSMenuSelected(app, event)
gps1=[app.x;app.y];
plot(app.UIAxes,app.x,app.y,'LineWidth',2);
hold on
plot(app.UIAxes,app.x( app.Slider ),app.y(app.Slider ),'b--o',...
'LineWidth',6)
hold off
end
I use hold on but when I try to use the slider I get an error
Array indices must be positive integers or logical values.
Error in OperatorMachine01/GPSMenuSelected (line 671)
plot(app.UIAxes,app.x( app.Slider ),app.y(app.Slider ),'b--o',...
Error using matlab.ui.internal.controller.WebMenuController/fireActionEvent (line 67)
Error while evaluating Menu Callback.

Accepted Answer

Geoff Hayes
Geoff Hayes on 15 May 2020
Lev - why in your SliderValueChanged callback are you overwriting the Slider with its value? I suggest that you remove this function (or at least the code inside of it)
function SliderValueChanged(app, event)
% app.Slider = app.Slider.Value; % <----- this isn't necessary and is dangerous!
end
As for the error, it is telling you that you are trying to index into an array with something other than a positive integer or (0 or 1) logical value. I suspect that the slider value is NOT an integer so you will need to ask your self how to convert this number into a valid index for the arrays. For example, your code might look like
function GPSMenuSelected(app, event)
gps1=[app.x;app.y];
plot(app.UIAxes,app.x,app.y,'LineWidth',2);
hold on
sliderValue = ceil(app.Slider.Value); % <--- round up to the nearest integer
if sliderValue >= 1 && sliderValue <= length(app.x)
plot(app.UIAxes,app.x(sliderValue),app.y(sliderValue),'b--o',...
'LineWidth',6);
end
hold off
end
I don't know if that is valid for you so please validate before trying it out.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by