How I make the first column increase in the arrayed separated with comma?

Hi
I hava an nodes of elements from text file sorted in array with x,y,z points. I would like to add an extra column which represent counter of the node. Each node needs to be separated by comma.
Ex:
nodes form text file sorted as follow:
2 4 5
4 3 3
2 6 9
8 6 1
I need to add extra column:
1 2 4 5;
2 4 3 3;
3 2 6 9;
4 8 6 1;
also, needs to separated with comma:
1, 2, 4, 5;
2, 4, 3, 3;
3, 2, 6, 9;
4, 8, 6, 1;
I write a code like this:
node = load('nodes.txt') ; % Load nodal coordinates from node.txt
numnode = size(node,1) ;
node = [zeros(numnode,1) , node];
dlmwrite('nodes.txt',node, 'delimiter', ',')
dlmwrite('nodes.txt',node, 'delimiter', '\t')
type('nodes.txt')
However, I have an issues writing like this:
  1. the new column is zeros. and I need it to be counted from 1 to the end of nodes.
  2. After the file was written it showed as one element reported by comma, while if I open it in excel for example I would like to see it each column in separate line (4 lines).
see the files.
Thank you.

 Akzeptierte Antwort

Instead of
node = [zeros(numnode,1) , node];
you would use
node = [(1:numnode).' , node];
You have two dlmwrite() to the same file. You should remove the one that uses tab separator.
In your description you show a semi-colon in your output. Is that going to be needed in the output text, or just comma separator?
To be able to read the output using that obsolete program notepad, you will need to add
'newline', 'pc'
to your dlmwrite() call. However, I would really recommend you upgrade to a more modern viewing program such as Notepad++ ... or really anything newer than Notepad. Like the 1996 Notepad+ . Requiring CR/LF line termination has been functionally obsolete since before Windows XP.

2 Kommentare

Thank you Walter for help.
I just need comma separator, however, after all it comes to one line each like:
1,-4.286,7.5822,-2.5716
2,-5.5612,6.2483,-3.5447
3,-4.9236,6.9152,-3.0581
While I need it to be separated from each number in the line as will
1, -4.286, 7.5822, -2.5716
so if I open it in excel it will appear each number in one cell. without the comma.
Change the file name to 'nodes.csv' . Or, when you go through the Import Wizard in Excel and tell it that it is a delimited file, on the next screen click on comma as the delimiter.
The problem is not with needing a space (you do not): the problem is that when you use a .txt format the default delimiter suggested by Excel is Tab.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by