Looping the data along with deliminaters (please find the code below)

조회 수: 1 (최근 30일)
Rakesh Yadav Kodari
Rakesh Yadav Kodari 2019년 2월 19일
댓글: Stephen23 2019년 2월 20일
I have to read data from the serial port multiple times like
A = fscanf(t)
B = fscanf(t)
C = fscanf(t)
D = fscanf(t)
E = fscanf(t) and so on i had to do it manually. so i tried the code below for making it simple, and also i have deliminaters for example M and L
n = 10000;
% while loop execution
while( n < 10000 )
A= fscanf(t);
end
but it is not working could any one please help me with the same
  댓글 수: 3
Bob Thompson
Bob Thompson 2019년 2월 19일
Do you know how many sets of data you're going to get? If so, yes it is possible to use a for loop. If not, a while loop is likely a better option.
Rakesh Yadav Kodari
Rakesh Yadav Kodari 2019년 2월 19일
I dont know how many sets of data I am going to get, but i know where the data starts and ends. how can i use a while loop?

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

답변 (1개)

Bob Thompson
Bob Thompson 2019년 2월 19일
편집: Bob Thompson 2019년 2월 19일
You were on the right track with your loop that you had already, the biggest thing would be to add an index.
i = 0; % Initalize your index
while n < 10000; % Loop break condition
i = i+1; % Change index to next value
A(i) = fscanf(t); % Record data to index
n = ... % You need to define n. How will you break your loop? Check for end of data set maybe?
end
  댓글 수: 3
Rakesh Yadav Kodari
Rakesh Yadav Kodari 2019년 2월 20일
Is there any possible way that i can change the vector itself like A,B,C,D....to save the ans?
Stephen23
Stephen23 2019년 2월 20일
@Rakesh Yadav Kodari: the default format for fscanf for serial objects is '%c'. As far as I can tell, this means that you could easily have multiple characters. Multiple characters cannot be allocated to one element of a char/numeric array. You could easily use a cell array, or check the size of the output before allocating to the array, or perhaps you just need to specify the format string to convert the data to numeric: which of these you need depends on your data and your algorithm, which you have not told us about.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by