Clear Filters
Clear Filters

App Designer - Adding two variables halts process

1 view (last 30 days)
I have a timer which ticks every 0.5s. It calls a function which adds two numbers, the sum of which is assigned to one of those numbers.
E.g.
x = x + y;
For some reason however, nothing will run after this line. Nothing. It just stops. I get rid of this and everything below works fine. What is going on?
I have a function that is supposed to be called after this line which never gets called if I have this addition above it. I remove the addition, the function then calls. I need the addition but having it just breaks it.
  1 Comment
Geoff Hayes
Geoff Hayes on 11 Apr 2022
@Byron Piper - can you show us the callback function and the code that is called? Are you sure there are no errors in the console window? Are x and y just floating-point variables or are they some other data type?

Sign in to comment.

Answers (1)

Abhishek Chakram
Abhishek Chakram on 28 Sep 2023
Hi Byron Piper,
It is my understanding that you are facing difficulty setting up a timer and it’s callback in the App Designer. Here is a sample code for the same:
% Initialize variables
x = 5; % Initial value of x
y = 3; % Initial value of y
% Create and start the timer
t = timer('ExecutionMode', 'fixedRate', 'Period', 0.5, 'TimerFcn', @timerCallback);
start(t);
% Timer callback function
function timerCallback(~, ~)
% Add y to x and assign the sum back to x
x = x + y;
% Call a function after the addition
myFunction(x);
end
% Function called after the addition
function myFunction(value)
disp(['The value after addition is: ' num2str(value)]);
end
in this example a timer is created which executes a callback ‘timerCallback‘ that updates the value of x. You can add this [AJ1] to code to any buttonPushedCallback or startupFcn callback.
Best Regards,
Abhishek Chakram

Categories

Find more on Software Development Tools in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!