Can't get variable out of Callback function. Already defined as global.
    7 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
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 comentarios
Respuestas (1)
  Image Analyst
      
      
 el 29 de Mayo de 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 comentarios
  Image Analyst
      
      
 el 30 de Mayo de 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.
Ver también
Categorías
				Más información sobre Signal Integrity Kits for Industry Standards en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!