How to fprintf one large variable that is a table?

I have a variable "fluence_rate" in my workspace that is a 24056x2000 table. How do I frpintf this and have it perfectly aligned column by column instead of one extremely long column? Also is it normal if it takes a really long time?

답변 (1개)

dpb
dpb 2018년 7월 12일
편집: dpb 2021년 8월 16일
nCol=10; % how many columns you want???
fmt=[repmat('%11.3E',1,nCol) '.\n']; % format string for that many
fprintf(fid,fmt,X.'); % print array, note transpose
Adjust the column and format to suit

댓글 수: 4

HC
HC 2018년 7월 17일
I don't find this answer to be very clear.
  1. Define the number columns you want in the output file (nCol)
  2. Create format string of that many fields and newline after
  3. Print the array (variable "X" here, use your desired array name)
That leaves only deciding what actual format you want the values printed at to set the specific field form, the '%11.3E' here, see the documentation for fprintf for details on format strings.
And, of course, there are several examples at
doc fprintf
and remember must get the file handle fid via fopen and then to fclose(fid) when finished.
djk
djk 2021년 8월 16일
Thank you DB! I used your solution to print arrays with variable column numbers to a file. I found a typo, though -
fprintf(fid,X.')
should be
fprintf(fid,fmt,X.')
Thank you again.
dpb
dpb 2021년 8월 16일
편집: dpb 2021년 8월 16일
Indeed, good catch...I'll fix the original to reflect.
BTW, NOTA BENE that normally the nCol would be size(x,2), the number of columns in the array itself -- but the Q? here with an array of 2000 seemed like might want to cut that down -- although if each column really were a variable, then it might still be the right thing to do, just that printing such a large array in human-readable text format is the questionable decision.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

HC
2018년 7월 12일

편집:

dpb
2021년 8월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by