MATLAB: Write matrix into .txt file

countermatrixtext file

i have a matrix like this
A=
10 5 0
12 7 3
15 8 5
19 9 6
i want to add a letter before each element in this matrix , while first column is x-axis values , 2nd column is y-axis values and 3rd column is Z-axis values.
i need to write a capital letter of X before each value of first column elements and letter Y before each value and the same with 3rd column with Z letter like this
X10 Y5 Z0
X12 Y7 Z3
X15 Y8 Z5
and so on. And after that i need to add this word of ( G1 ) before each line ( or each row of the matrix ) like this
G1 X10 Y5 Z0
G1 X12 Y7 Z3
G1 X15 Y8 Z5
and last thing to add in capital letter of N followed by the number of the line like this
N1 G1 X10 Y5 Z0
N2 G1 X12 Y7 Z3
N3 G1 X15 Y8 Z5
and save all of these into text file editable with notepad and other software
i need to know what is the functions to use and how to apply them to make this edit i want
thanks
Tarek

Best Answer

Hi Tariq
Perhaps you may find convenient generating a work space variable containing all the data before writing to text file.
clear all;close all
A= [ 10 5 0; 12 7 3; 15 8 5; 19 9 6]
[s1 s2]=size(A)
A3=cell(s1,s2)
str1='XYZ'
for k=1:1:s2
for s=1:1:s1
A3{s,k}=[str1(k) num2str(A(s,k))]
end
end
header1=cell(s1,1)
for k=1:1:s1
header1{k}=['N' num2str(k)]
end
header2=cell(s1,1)
for k=1:1:s1
header2{k}='G1'
end
T = table(header1,header2,A3(:,1),A3(:,2),A3(:,3));
Now the data to write to text file is contained in table T
T =
4×5 table
header1 header2 Var3 Var4 Var5
_______ _______ _____ ____ ____
'N1' 'G1' 'X10' 'Y5' 'Z0'
'N2' 'G1' 'X12' 'Y7' 'Z3'
'N3' 'G1' 'X15' 'Y8' 'Z5'
'N4' 'G1' 'X19' 'Y9' 'Z6'
One can access fields of the table in same way as arrays and cells
T(1,1)
=
table
header1
_______
'N1'
T(1,3)
=
table
Var3
_____
'X10'
T(3,:)
=
1×5 table
header1 header2 Var3 Var4 Var5
_______ _______ _____ ____ ____
'N3' 'G1' 'X15' 'Y8' 'Z5'
T(:,4)
=
4×1 table
Var4
____
'Y5'
'Y7'
'Y8'
'Y9'
writing to .txt file
writetable(T,'file1.txt','WriteVariableNames',false,'WriteRowNames',false,'Delimiter',' ')
Also possible, saving to .mat file instead of text tile.
save('table1.mat','T')
John BG