Clear Filters
Clear Filters

DAQ cannot generate signal in GUI callback function.

2 views (last 30 days)
Hi,
Currently I have these code in a .m file and it works properly by continuing generating signal.
s = daq.createSession('ni');
addAnalogOutputChannel(s,'cDAQ1Mod2',0,'Voltage');
s.IsContinuous = true;
s.Rate = 10000;
data = linspace(-1,1,5000)';
lh = addlistener(s,'DataRequired', ...
@(src,event) src.queueOutputData(data));
queueOutputData(s,data)
startBackground(s);
However, when I copy these code into a pushbutton callback function in my GUI. No signal being generated.
s = daq.createSession('ni');
addAnalogOutputChannel(s,'cDAQ1Mod2',0,'Voltage');
s.IsContinuous = true;
s.Rate = 10000;
data = linspace(-1,1,5000)';
lh = addlistener(s,'DataRequired', ...
@(src,event) src.queueOutputData(data));
queueOutputData(s,data)
startBackground(s);
pause;
After I added 'pause' at the end as the above code, it starts works properly. But this is not the way I want to do. I hope anyone can help me by explaining the reason and want to know is any way that I can generate the continuous signal by using GUI and stop generating whenever user wants to.
Thanks so much!

Answers (1)

Geoff Hayes
Geoff Hayes on 13 Aug 2019
Bohan - I suspect that what is happening is that because you are creating local variables for the DAQ session, then when the callback completes, the local variables are destroyed and your DAQ session ends. That is why calling pause "fixes" the problem - the function doesn't complete and the local variables do not go out of scope. If you are using GUIDE, then save the DAQ session (and probably listener) to the handles structure so that they are maintained. You can then later end the DAQ session when needed by accessing it via this structure.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!