Can't get variable out of Callback function. Already defined as global.
Show older comments
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 Comments
Walter Roberson
on 29 May 2022
Could you confirm that in your main script you also declare data as global there?
Kaan Inal
on 29 May 2022
Kaan Inal
on 29 May 2022
Walter Roberson
on 29 May 2022
processedData = data;
That is not going to update processedData except when the statement is executed. as data changes processedData is going to stay whatever size last assigned. When you start out the global variable data is going to be empty until you assign something else to it.
I suggest that initialize processedData as zeros(0,N) where N is the number of samples per reading that you expect.
The fscanf is going to grab all available samples until Terminator or timeout, which is not necessarily absolutely going to be the same number each time, so really you should be checking the size of data before you store.
Remember too that you might have multiple readFrame callbacks between places the main script processes the data. Including possibly none.
Are you familiar with the concept of ring buffers? I recommend them for any asynchronous data processing that needs to process each input exactly once.
Kaan Inal
on 29 May 2022
Walter Roberson
on 29 May 2022
Edited: Walter Roberson
on 29 May 2022
if read offset is the same as write offset then the buffer is empty
otherwise pull the data out from read offset, and set read offset = mod(read offset, buffer size) + 1
similar logic for writing, write at write offset, mod+1 to get the new write offset.
Kaan Inal
on 29 May 2022
Walter Roberson
on 29 May 2022
you would still have a call back function that stored the values in the circular buffer.
Kaan Inal
on 29 May 2022
Kaan Inal
on 29 May 2022
Answers (1)
Image Analyst
on 29 May 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 Comments
Kaan Inal
on 29 May 2022
Image Analyst
on 29 May 2022
What line does it hit? And what are the sizes of processedData and data at that point?
Kaan Inal
on 29 May 2022
Image Analyst
on 29 May 2022
Then you need to figure out why
message = read(src,payloadBytes,"uint8");
always returns null. We can't do that for you.
Kaan Inal
on 29 May 2022
Image Analyst
on 29 May 2022
Edited: Image Analyst
on 29 May 2022
Then why did you say "data is always empty" when it's clearly not. It has 8384 values in it. Or it somehow became null before it go to the line where it's supposed to assign processedData. You need to find out why and where it went from 8384 elements to null.
Kaan Inal
on 29 May 2022
Image Analyst
on 30 May 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.
Kaan Inal
on 31 May 2022
Categories
Find more on Signal Integrity Kits for Industry Standards 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!