Callback function after addlistener for National Instruments board

Probab not really a Matlab question:
After daq sessions are set (getting data from a NI card):
devices = daq.getDevices;
DataSession = daq.createSession('ni'); % start a session with the National Instruments
and addlistener is called:
lh0 = addlistener(DataSession,'DataAvailable',@(src, event)logData_v1p1(src, event, fid1));
It works, but I cannot find ANYWHERE the file logData_v1p1 which logs data to the file (fid1).
I've searched all Matlab folders, subfolders, etc., and files I've put in "Box". Where might logData_v1p1 be?
OR: how can I search properly??
OR: if I just want to "massage" the data before it's written to a file, how I access the data, massage it, and then write it to a file?

답변 (1개)

Steven Lord
Steven Lord 2025년 4월 11일

0 개 추천

The only place where a search of the MathWorks website finds that function name is this post. Can you tell us where you found that addlistener call that you are asking about, the one where logData_v1p1 is called? Was it in an example (potentially from an older release), was it something that you found elsewhere online, was it something you got from your professor (if you're a student) or from a colleague, was it something hallucinated by ChatGPT, etc.?

댓글 수: 5

I'm quite sure this was a local program written earlier by someone in the lab, but I don't know who. If the voltage data from a NI card is to be written to a file, then what standard Matlab program is normally used in addlistener instead of logData_v1p1?
I think the answer to your follow-up question is "It depends." You state that you want to write the data to a file: what type of file? This documentation page lists five different categories of "common file formats": do you want to write to a plain text file, a formatted file (and if so which format: CSV, XML, JSON, etc.), an Excel spreadsheet, etc.? Or do you want more fine-grained control over exactly how the data is written, in which case you might want to directly call the low-level file I/O functions.
Once you know what type of file you want to write, click on the Standard File Formats link on that page (or the Low-Level File I/O link if that's what you want) and then the category for the type of file you want to write on the resulting page. That will show you the functions you can use to write your listener.
Thanks Mr. Lord for being so helpful and patient. Probably even more knowlegable than ChatGPT (but maybe not quite as fast!)
Currently, the data is written to a binary file as a double, and then later for analysis, plotting, etc., read back into Matlab. If I want to use fwrite instead of logData_v1p1, then I could use (does this look right??):
lh0 = addlistener(DataSession, 'DataAvailable', ...
@(src, event)logDataWithFwrite(src, event, fid1));
with the function logDataWithFwrite defined as:
function logDataWithFwrite(~, event, fid)
% Log data to the binary file using fwrite
% 'event.Data' is a matrix of data
% Write the data as binary in column-major order (default for MATLAB)
fwrite(fid, event.Data', 'double'); % Transpose to ensure correct order
end
At first glance, assuming you've created fid1 using fopen before adding the listener, I think that's correct. I haven't worked with Data Acquisition Toolbox so I'm not 100% certain.
Right. Thanks again.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

릴리스

R2024a

질문:

2025년 4월 11일

댓글:

2025년 4월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by