App Designer - Adding two variables halts process

2 ビュー (過去 30 日間)
Byron Piper
Byron Piper 2022 年 4 月 10 日
回答済み: Abhishek Chakram 2023 年 9 月 28 日
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 件のコメント
Geoff Hayes
Geoff Hayes 2022 年 4 月 11 日
@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?

サインインしてコメントする。

回答 (1 件)

Abhishek Chakram
Abhishek Chakram 2023 年 9 月 28 日
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

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by