Counter Values Are Not Changing, MATLAB

조회 수: 1 (최근 30일)
Daniel Barzegar
Daniel Barzegar 2014년 6월 18일
댓글: Daniel Barzegar 2014년 6월 18일
I am new to Matlab and I have an issue where two counters in my code do not change value.
fid = fopen('results.txt', 'wt');
counter1 = 0;
counter2 = 3;
for i=1:size(res_t,1)
counter1 = counter1 + 1;
if(counter2 == 3)
fprintf(fid, '%f ', res_t.time(:,:)); % first column i need the time
counter2 = 0;
end
fprintf(fid, '%f ', res_t.data(:,:)');
if(counter1 == 3)
fprintf(fid, '\n'); %after it writes all 3 X,Y,Z i want to change line
counter1 = 0;
end
counter2 = counter2 + 1;
end
fclose (fid);
}
When compiled, the values of counter1 & counter2 do not change, which means they never meet the If statement conditions. My output file is a file which contains: column1: time values, columns2 to 4: X, Y, Z coordinates respectively.
For example:
Time1 X1 Y1 Z1
Time2 X2 Y2 Z2
Thanks!

채택된 답변

dpb
dpb 2014년 6월 18일
Probably because
size(res_t,1)
is either empty or 0 so the loop never executes.
You say "when compiled"; is that a euphemism or are you really building a compiled .exe? I've never had the toolbox so don't know what/how it deals with undefined variables when it finds one--at the command line if res_t didn't exist you'd error and know what the problem is.
  댓글 수: 4
Daniel Barzegar
Daniel Barzegar 2014년 6월 18일
well, my matrix has 9 lines and i want to do the for loop for all the lines. How i can do the for loop until the end of the matrix?
for i=1: ???
... ...
end
cheers
José-Luis
José-Luis 2014년 6월 18일
By lines you mean row or columns?

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

추가 답변 (2개)

Image Analyst
Image Analyst 2014년 6월 18일
Why not just write out all 4 numbers in a single fprintf()???? Also, you'd have to index res_t with i, which you're not doing now.
  댓글 수: 2
Daniel Barzegar
Daniel Barzegar 2014년 6월 18일
because this is just a small example . my real file has more than 5000 samples...!
Image Analyst
Image Analyst 2014년 6월 18일
5000 is still pretty small.

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


dpb
dpb 2014년 6월 18일
...the value of size(res_t,1) = 1 , that's why i doesnt do the loop. res_t is a matrix having 9 lines
Well, one or the other of those statements is just plain wrong. Use the debugger and step thru and figure out where it goes wrong or show enough context we can tell -- as is, there's not enough information to do anything useful on that part.
Oh, I see...you've written
for i=1:size(res_t,1)
but it turns out looking on down later that res_t is a structure not matrix. You need
for i=1:size(res_t.data,1)
to address the data array inside the structure.
And, as IA indicates, why not write the file with one of the higher-level routines like dlmwrite or at least with a single fprintf loop?
dlmwrite('yourfile',[res_t.time res_t.data], 'delimiter', ' ')
  댓글 수: 1
Daniel Barzegar
Daniel Barzegar 2014년 6월 18일
yes, you are right. i just had to have size(res_t.data). thanks!

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

카테고리

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