alternatives to putting multiple placeholders
조회 수: 1 (최근 30일)
이전 댓글 표시
I have an 1000000*8 matrix which I am trying to output to a text file.
fprintf(fid, '\n%12d %12d %12d %12d %12d %12d %12d %12d', P(:,1:8)')
If the matrix had more columns I would be writing the same placeholder (%12d) many more times. Is there a more efficient way to copy a large matrix to a text file preserving the rows and columns, possibly by writing the necessary placeholder just once.
댓글 수: 0
답변 (2개)
Walter Roberson
2012년 3월 13일
No there is not.
You can, however, pre-calculate the format as a string and pass the string in to the call.
numcols = 8;
fmt = [ '\n', repmat('%12d ', 1, numcols) ];
fprintf(fid, fmt, P(:,1:8).');
댓글 수: 0
Jacob Halbrooks
2012년 3월 13일
Since all of your data has the same format, you can let FPRINTF repeat the format specifier over all of your data. Here is an example of doing that:
data = rand(5,10);
for rInd = 1:size(data,1)
fprintf('\n');
fprintf('%12d ',data(rInd,:));
end
If your data is not regular enough for this, another approach would be to use REPMAT to dynamically create the format specifier for your data. I wrote an example of that here.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!