Question about delimiter for dlmwrite

Hello,
I want to seprate matrix elements in 8 spaces. However, when I wrote
dlmwrite( 'A.txt', Matrix, 'delimiter',' ')
error was occurred because delimiter requires a single character. How can I apply 8 spacesto separate matrix elements?

 Réponse acceptée

Walter Roberson
Walter Roberson le 16 Nov 2011

1 vote

The documentation does imply that multiple characters should be acceptable.
Anyhow, you can use the Precision property to specify a format such as '%20.3f' . This is not exactly the same as leaving 8 characters between columns: it would be for using exactly 20 characters per column (in this example), which is a more typical requirement than 8 spaces between columns (which is difficult to keep consistent because different numbers print out with different widths.)

4 commentaires

Soyoung
Soyoung le 16 Nov 2011
Thank you for your idea. I am not sure if this was that you meant. I modified the statement as:
dlmwrite('file_egsphant.txt', sliced_matrix, ...
'-append', 'newline', 'pc', 'precision', ...
'%20.3f', 'delimiter', ' ' );
% ' ' means 8 spaces inbetween elements
I was hoping to see the results by adding percision, but it is not still working. Did I miss something wrong from your advice?
Soyoung
Soyoung le 16 Nov 2011
there is an error. On my comment after your answer, because the expression looked as a single space with ''. Only here, did I write '________' instead of '_'. '________' means simply 8 spaces. I just added under-bar for 8-spaces characeter but the real one in my code does not have under-bar between quotation marks. Thank you.
Walter Roberson
Walter Roberson le 16 Nov 2011
If you need a fixed number of spaces between entries, then you are better off constructing the file yourself using fopen(), fprintf(), fclose()
But do you really need exactly 8 spaces between the entries, even if the entries are not the same length? For example, using _ to represent space:
7.2________0________1.1
3________-5.89________123.456
Or do you instead want each column to occupy a total of 8 spaces, as in
_____7.2_______0_____1.1
_______3___-5.89_123.456
Soyoung
Soyoung le 16 Nov 2011
What I want to make is like the first thing of your examples:
0.0011________0.0011________0.0011.
While I was waiting an answer, I constructed fprintf function. It is working now but calculation time takes longer than dlmwrite.
So, if there is a way to treat this format in dlmwrite function instead of fprintf, then could you give me advice? Thank you very much for your comments.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by