Numeric Slider not changing in live editor
16 次查看(过去 30 天)
显示 更早的评论
I'm new to the live editor. I want to add a loop with slider bar variables. And as the slider bar changes, the plot will change. But if there is no change, I still want other things in the plot to update. Here's some sample code that replicates what I'm seeing:
while true
test1=7;
fprintf('%i',test1)
pause(1)
end
Except, where "test1=7" is, that is supposed to be a live editor numeric slider. But as I move that slider around, nothing happens. It just keeps printing "7" over and over. The slider controls are set to Run On "Value changing." And Run "Current section". The only thing that works is Run "All sections". But in my larger program, that is not what I want to do. It would take a long time to re-run everything just for one small variable change.
Is there anything I can do to make this work?
Thanks.
5 个评论
Stephen23
2023-11-23
"The reason for this is the plot is actually measuring real time sensor data."
Please show the code you use to import the sensor data with.
回答(1 个)
Zuber Khan
2023-12-14
Hi,
I understand that you want to update the variable "test1" using an interactive slider and also want to update the plot irrespective of whether the slider variable has changed or not.
In MATLAB's live editor, when using a "while true" loop, the code inside the loop will keep executing without interruption, and it won't be responsive to changes in the user interface, such as moving a slider. If you remove the while loop, the variable "test1" will update according to the slider.
However, since you have explicitly specified that you need to use "while" loop to selectively update the plot irrespective of the change in the value of slider variable, there is a possible workaround to use a "while" loop that checks for updates to the slider value at each iteration and updates the plot accordingly. Although, this approach requires manual coding of the slider functionality instead of using the built-in Live Editor slider, it will help to achieve the desired behaviour.
% Create a figure and a slider control
fig = uifigure;
sld = uislider(fig, 'Position', [100, 75, 120, 3]);
% Initialize variables
test1 = sld.Value; % Initialize test1 with the slider's value
data = measureSensorData(); % Initial call to get data
% Create a plot
hPlot = plot(data);
xlim([1, length(data)]); % Set the x-axis limits
% Set up the update loop
while true
% Check if the slider value has changed
if sld.Value ~= test1
test1 = sld.Value; % Update test1 with the new slider value
% Perform any updates that depend on test1
end
% Measure new sensor data
data = measureSensorData();
% Update the plot
hPlot.YData = data;
% Update the figure window
drawnow;
% Pause for a short period to prevent overwhelming the CPU
pause(0.1);
end
function data = measureSensorData()
data = rand(1,10);
end
Note that in the above code, I have defined "measureSensorData()" function randomly since it is not specified. You can update it accordingly to make it dependent on "test1" value as per your requirements.
I am also attaching the output of the code snippet as follows:
You can notice in the command window that altering the slider adjusts the "test1" variable's value accordingly, and the plot is updated regardless of any changes to the "test1" slider variable.
In case, you want the plot to change with the slider variable interactively, you can refer to the following documentation link:
To know more about creating the slider manually using the "uislider" function, kindly refer to the following documentation link:
I hope it resolves your query!
Best Regards
Zuber
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!