필터 지우기
필터 지우기

Strange file output while using fprintf

조회 수: 1 (최근 30일)
Hissam Aziz
Hissam Aziz 2013년 4월 3일
Hi Everyone, thank you in advance for your help. I'm trying to write two columns in a txt file using fprintf.
fprintf(filesave2,'%d %\r\n', X, Y);
I even tried manipulating the numbers of characters i wanted to print:
fprintf(filesave2,'%3d %7.5f\r\n', X, Y);
but all in vain. It keeps giving me very strange results.
Results:
9 9.00000
9 9.00000
9 9.00000
..... and so on.
My original data
looks like this:
X is a vector from 5 to 300 with intervals of 5. i.e. 5 10 15 ... 300
Y is a vector with power values 0.46957 0.41538 0.37951 .. etc (60 values).
How can i fix this? So that the written file looks like:
5 0.46957
10 0.41538
15 0.37951
...
etc
Thank you so much for your help!

채택된 답변

Walter Roberson
Walter Roberson 2013년 4월 3일
fprintf(filesave2,'%d %f\r\n', [X(:), Y(:)].' );
  댓글 수: 3
Walter Roberson
Walter Roberson 2013년 4월 3일
편집: Walter Roberson 2013년 4월 3일
Please re-check that X and Y have exactly the same number of elements. Whether they are row vectors or column vectors or matrices does not matter for the code I gave, as long as the number of them is the same.
The .' is matrix transpose. The first part, [X(:), Y(:)] is forming an array of two columns, X and Y, and the .' flips that around so that X becomes the first row and Y becomes the second row. You need to do this for your fprintf() because fprintf() follows values down the columns -- so you want the first column to have an X value and then a Y value (to print them side by side), and the next column to have the second X and Y values, and so on.
If you knew for sure that X and Y are row vectors, then you could instead have coded as
fprintf(filesave2,'%d %f\r\n', [X;Y] );
Hissam Aziz
Hissam Aziz 2013년 4월 3일
편집: Hissam Aziz 2013년 4월 3일
Ahh fixed it! Worked like a charm!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by