fprintf In column wise

37 ビュー (過去 30 日間)
Sunag R A
Sunag R A 2016 年 12 月 17 日
編集済み: Jan 2016 年 12 月 18 日
Hi, I have got one problem with respect to printf command while arranging the matrix in column wise for ".dat" file.Here are the details:
I have got the variables as
X = 0 0.1500 0.3000
0 0.1500 0.3000
0 0.1500 0.3000
and
Y = 0 0 0
0.1500 0.1500 0.1500
0.3000 0.3000 0.3000
and I need the arrangement in the "dat" file as
X Y
0 0
0.15 0
0.30 0
0 0.15
0.15 0.15
0.30 0.15
0 0.30
0.15 0.30
0.30 0.30
I tried this command :
fprintf(fid,'%f\n%f\n%f \n',[Y(:)',X(:)']); %%Say i have fopen then inserted into it with fileID
But the result I am getting is in a single column as Total X terms first and Total Y terms second,
0
0.15
0.30
0
0.15
0.30
0
0.15
0.30
0
0
0
0.15
0.15
0.15
0.30
0.30
0.30
Please help me out to make it correct in the write format. I tried using \t in between, But in "dat" file, it creates disturbance to upper commands if I use that.
Thanks and Regards,
Sunag R A.
[EDITED, Jan, code formatted]
  1 件のコメント
Jan
Jan 2016 年 12 月 17 日

サインインしてコメントする。

回答 (2 件)

Jan
Jan 2016 年 12 月 17 日
編集済み: Jan 2016 年 12 月 17 日
fprintf(fid, '%g %g\n', [Y(:), X(:)].')
or:
fprintf(fid, '%g %g\n', [Y(:).'; X(:).'])
Perhaps you want '%f' instead of '%g'. Your format string did not match your wanted result and the values must be concatenated differnetly. But you have been very near already. With a little bit more trying you would have found it soon.
  2 件のコメント
Sunag R A
Sunag R A 2016 年 12 月 17 日
編集済み: Jan 2016 年 12 月 17 日
Hi,
Thank you very much for the reply. But this "%g" is giving me only a precision integer and not making two different columns even if the variable codes are same. I'm still in a confusion state of how to execute it.
Thanks and Regards,
Sunag R A.
Jan
Jan 2016 年 12 月 17 日
編集済み: Jan 2016 年 12 月 18 日
No, the code I've posted creates 2 columns and the number of columns is not related to the format. This only depends on the number of outputs before the '\n' appears in the format string. Simply copy&paste my code and run it. The '%f' or '%g' changes only the number of decimals for integer values.
Please do not insert blanks infront of a text here. This triggers a formatting.

サインインしてコメントする。


David Barry
David Barry 2016 年 12 月 17 日
編集済み: David Barry 2016 年 12 月 17 日
Assuming that x and y are always vectors of the same length then you could do something like this to merge them into a single variable and then call fprintf using this new variable.
reshape([X;Y], 1, numel([X;Y]))
  3 件のコメント
Jan
Jan 2016 年 12 月 17 日
@Sunag R A: did you see, that your code in the question is badly formatted? It is impossible to guess, what you want to achieve: Please read the "?" link to learn how to format text and code in this forum.
David Barry
David Barry 2016 年 12 月 17 日
What Jan Said. My answer was based on your original post which made it look like vectors.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by