Problems with function handle in Appdesigner
이전 댓글 표시
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
답변 (1개)
Markus Langer
2020년 5월 13일
댓글 수: 3
Joris Lambrecht
2021년 2월 5일
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
2021년 6월 11일
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
2022년 4월 26일
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.
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!