How to make Nested while loops faster

조회 수: 1 (최근 30일)
Maheen Siddiqui
Maheen Siddiqui 2016년 4월 4일
댓글: Maheen Siddiqui 2016년 4월 5일
I am running a while loop that is scanning the serial port for data. This while loop is inside a bigger while loop that is continuously acquiring data from an instrument (a spectrometer).
The smaller while loop that scans the port continuously writes to a file until the while condition becomes False. I have noticed that this while loop takes so much time that it interferes with the data acquisition from the spectrometer. I use the commands fread(s) to read from serial port object s and fwrite to write to file. Both these processes seem extremely slow and compromise data acquisition.
Is there a way to speed this up? Is there something I can use that is faster than fread? Should I be writing an array instead of to a file? If so, how do I write to an array during a while loop where I'm unsure of how big the array will be beforehand...

답변 (1개)

Walter Roberson
Walter Roberson 2016년 4월 4일
It sounds as if you should be using a BytesAvailableFcn to trigger a callback when there is data available, and when there is, read BytesAvailableFcnCount worth of data.
  댓글 수: 1
Maheen Siddiqui
Maheen Siddiqui 2016년 4월 5일
Thanks for your reply. Do you think the fread should be initiated inside the callback function? Or in the script where the serial object is defined and opened and that calls the callback function?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by