How do I print a formatted matrix without using a loop?

45 ビュー (過去 30 日間)
Kevin
Kevin 2013 年 10 月 5 日
編集済み: Cedric 2013 年 10 月 5 日
I am writing a matrix equation to a txt file using fprintf. I'm using %f to specify the vairable type. When it prints, however, it prints the elements in a list vertically going down the page. I would like to display the matrix (which is a 6x6) so that it maintains the row/column structure. Is there a way to do this simply? Preferably without a loop? Thank you for the help.

回答 (2 件)

cr
cr 2013 年 10 月 5 日
編集済み: cr 2013 年 10 月 5 日
Why not
disp(m).
%f will sequentially output all nos of the matrix. If you are using textile output, just use
dlmwrite('filename.txt',m,' ')
for space delimited text file.
Cheers.

Cedric
Cedric 2013 年 10 月 5 日
編集済み: Cedric 2013 年 10 月 5 日
fprintf( fid, '%f %f %f %f %f %f\n', m.' ) ;
where fig is the file identifier returned by FOPEN (or 1 if you want to test on stdout/screen). What you didn't understand is that FPRINTF reads arrays linearly (column first) and repeats the formatSpec as many times as required for outputting all elements of the array.
In this solution, we pass the transpose of m to FPRINTF so column-first becomes row-first, and we put six %f in the formatSpec before the \n which repeats the row output (6 elements and a line return).

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by