How can I pass a variable (numeric) through a callback event when I pressed a key in my figure?
    6 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
Hello,
I have an issue;
I am collecting data in a while loop and I want to store this data in my callback event when I press a key in my figure.
This does not work at all.
Could you please help me in this topic?
My code example:
set(gcf, 'KeyPressFcn', {@GetData, calibration});
while true
    pause(0.00001);
    [calibration] = ml_CANapeReadCalibrationObject(moduleHandle(1,2), 'F_DE_f_1', 1);
    sensorData = [sensorData; calibration(1)];
    plot(sensorData);
    drawnow;
    disp('getting data');
end
function GetData(src, event, sensorData)
finalData = [finalData; sensorData];
pause(1);
end
2 Commenti
  Jan
      
      
 il 20 Ott 2021
				This piece of code is not running. It is not clear if the callback is a nested function or not. "Does not run at all" is not useful to explain the problem. Do you get an error message?
Risposte (2)
  Walter Roberson
      
      
 il 30 Apr 2025
        set(gcf, 'KeyPressFcn', {@GetData, calibration});
That code does not bind "calibration" to whatever the then-current value of the variable "calibration" is. Instead, it reads the value of the variable "calibration" as of the time that the set() is performed, and sets the KeyPressFcn to use that recorded version of "calibration".
There is no syntax for setting the KeyPressFcn for retrieving the then-current value of a variable. You would have do something such as continually assigning the variable in the 'base' workspace, and telling the KeyPressFcn to evalin('base')
finalData = [finalData; sensorData];
finalData is not defined inside of that function. You would have to something such as making it a shared variable and making GetData a nested function.
0 Commenti
  Vedant Shah
 il 30 Apr 2025
        To store the data present in “sensorData”, it is essential to ensure that the callback function receives the correct value of the “sensorData” variable. The following approach can be utilized which makes use of “setappdata” and “getappdata”: 
Firstly, modify the setting line to ensure that no data is passed in the callback function: 
set(gcf, 'KeyPressFcn', @GetData); 
The “setappdata” function can be used to store the variable “sensorData” . It can be added just before plotting it: 
setappdata(gcf, 'sensorData', sensorData);  
Additionally, the “GetData” function can be modified to make use of “getappdata” and to ensure that the “finalData” variable remain conistent: 
function GetData(src, event) 
sensorData = getappdata(src, 'sensorData'); 
persistent finalData 
if isempty(finalData) 
    finalData = []; 
end 
finalData = [finalData; sensorData]; 
pause(1); 
end 
The use of the “persistent” keyword ensures consistency when saving values multiple times during code execution. 
For further information, refer to the following MATLAB documentation links: 
0 Commenti
Vedere anche
Categorie
				Scopri di più su Structures in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!