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

counterelementmatrix array

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.

Best Answer

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.