Can't get variable out of Callback function. Already defined as global.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a serialport bytesavailable callback function.
function readFrame(src,~)
global data
global payloadBytes
message = read(src,payloadBytes,"uint8");
src.UserData = message;
data = message;
return
I read the buffer into the variable message and then save it as global variable data.
i get the variable in my workspace. Then something happens that I can't explain. I try to svae this global variable in a local variable in my Mainscript.
processedData = data;
Problem is processedData is always empty. So when i want to make a 2D Matrice to save different data liek this;
processedData(frameCount,:) = data;
I get an exception: indices doesn't match. Thats no wonder.
Can somebody tell me what I am doing wrong.
Thank you.
10 commentaires
Réponses (1)
Image Analyst
le 29 Mai 2022
Try assigning it like this
if isempty(data)
fprintf('Skipping frameCount = %d because data vector is empty!\n', frameCount);
elseif processedData <= 1 || isempty(processedData)
% The first time through just assign it so that processedData has the same number of columns as data
processedData = reshape(data, 1, []); % Make sure it's a row vector.
else
% Append on data into a new row for the second and subsequent times through.
processedData(frameCount,:) = data;
end
9 commentaires
Image Analyst
le 30 Mai 2022
You need to get it visible in the same scope. For example attach it to the app structure, like
app.data = read(src,payloadBytes,"uint8");
Then app.data should be available everywhere because app is available everywhere. It's like a global variable.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!