Problems with function handle in Appdesigner
Show older comments
I am currently trying to create my first GUI with Appdesigner. I have a Polar Chest Strap (BLE) that I am reading out. A working (plain)Matlab minimal example is below.
Unfortunately, trying to import the functionality into Appdesigner fails, maybe because I have not done anything with object-oriented programming so far. The strap is recognized after the scan and I can connect; if data is pending, the specified function handle would have to be called and the light would have to be set to green. But this does not happen. Maybe someone can help me.
Here again the functional minimal example in Matlab:
clear all
clc
%% Connect
h10 = ble("EE3CC81051AD"); % Connect to Polar H10
hr_char = characteristic(h10,"Heart Rate","Heart Rate Measurement");
hr_char.DataAvailableFcn = @displayCharacteristicData;
function displayCharacteristicData(src,~)
[data,timestamp] = read(src,'oldest');
disp(data);
disp(timestamp);
end
Answers (1)
Markus Langer
on 13 May 2020
3 Comments
Joris Lambrecht
on 5 Feb 2021
the problem, I think, is that @displayCharacteristicData should be @app.displayCharacteristicData or equivalently {@displayCharacteristicData, app}
and displayCharacteristicData(app) should be displayCharacteristicData(app, src, evt)
This is the syntax when using timer or serial callbacks. However this doesn't seem to work for the ble notification DataAvailableFcn callback and you will get the following error:
Invalid value for DataAvailableFcn. Specify [] or a function handle that accepts two inputs.
I left a comment in another post
"Saving data of command window from callback function"
hoping to learn why this doesn't work
Shad
on 11 Jun 2021
Has anyone figured out how to get BLE callbacks to work in App Designer? I can receive the data if I set the callback function to one in an external m-file, but that has other issues.
Suraj Parameswaran
on 26 Apr 2022
I am facing the exact same problem. I have come to understand that we cannot pass the app variable in the callback function because the function expects only a BLE characteristic object and an event as an argument.
I have tried. @Joris Lambrecht 's method and he has explained it didn't work. However, when I am defining the function inside a command button function as a nested function like below, I am able to see the output in the command window:
function ConnectButtonPushed(app, event)
function recv_data(data_channel,evt)
received = 1;
data_packet = char(data_channel.read('oldest'));
disp(data_packet);
end
data_channel = app.data;
app.data.DataAvailableFcn = @recv_data;
subscribe(app.data);
end
I am thinking the only way to work around this problem would be to use global variables and then extract the variables in them as the dataavailablefcn does not allow the 'app' to be passed in as an argument. Let me know if anyone has a better idea. Thanks.
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!