Replace strings with a different string and save the data

5 views (last 30 days)
I have a file for atomic coordinates, where I wanted to replace the 'c' with a certain pertange of maerials 'Fe', 'Al', 'Au'. The column C needs to be replaced with 40% Fe, 30% Al and 30% Au. Nothing else changes. The rest of the files stays same. I also plan to extend this to more elements lets say, Fe, Al, Ni, Cu, Zr all 20% each. I have attached a sample file. Below is a exceprt from the file. I will really appreciate any suggestions.
168
generated by VMD
C 0.000000 0.000000 0.000000
C -0.866025 0.500000 0.000000
C -0.866025 1.500000 0.000000
C 0.000000 2.000000 0.000000
C 1.732051 0.000000 0.000000
C 0.866025 0.500000 0.000000
C 0.866025 1.500000 0.000000
C 1.732051 2.000000 0.000000
C 3.464102 0.000000 0.000000
C 2.598076 0.500000 0.000000

Answers (2)

VBBV
VBBV on 9 Dec 2023
Edited: VBBV on 9 Dec 2023
D = readtable('data.txt');
C = char(['40% Fe';'30% Al']) % e.g. element data to be filled in 1st column
C = 2×6 char array
'40% Fe' '30% Al'
D.(1) = repmat(C,168/2,1) % replace 1st column of the table with element data
D = 168×4 table
Var1 Var2 Var3 Var4 ______ ________ ____ ____ 40% Fe 0 0 0 30% Al -0.86603 0.5 0 40% Fe -0.86603 1.5 0 30% Al 0 2 0 40% Fe 1.7321 0 0 30% Al 0.86603 0.5 0 40% Fe 0.86603 1.5 0 30% Al 1.7321 2 0 40% Fe 3.4641 0 0 30% Al 2.5981 0.5 0 40% Fe 2.5981 1.5 0 30% Al 3.4641 2 0 40% Fe 5.1962 0 0 30% Al 4.3301 0.5 0 40% Fe 4.3301 1.5 0 30% Al 5.1962 2 0
  2 Comments
VBBV
VBBV on 9 Dec 2023
You can rpleace the 1st column of the datatable with percentage of elements you want as vector shown above
Avik Mahata
Avik Mahata on 9 Dec 2023
I really appreciate your comment. I didn't do a good job explaining the problem. What I meant to say is , that 'C' will be replace by 2/3/4 different elements (characters) of centain pertanage. That means if I have 100 'C' rows in the first column, it will be replace by 40 Fe, 30 Al and lets say 30 Ni. I should have a way to change the amount/percentage I want to change 'C' in the first column.
So basically what I have is below,
168
generated by VMD
C 0.000000 0.000000 0.000000
C -0.866025 0.500000 0.000000
C -0.866025 1.500000 0.000000
C 0.000000 2.000000 0.000000
C 1.732051 0.000000 0.000000
What I want is this,
168
generated by VMD
Fe 0 0 0
Al -0.866025000000000 0.500000000000000 0
Ni -0.866025000000000 1.50000000000000 0
Fe 0 2 0
Al 1.73205100000000 0 0
Ni 0.866025000000000 0.500000000000000 0

Sign in to comment.


Voss
Voss on 21 Dec 2023
I assume that you want to replace the C's with the specified elements (e.g., Fe, Al, Ni) at random according to the specified proportions (e.g., 40, 30, 30).
% name of the original file:
filename = 'graphene.txt';
% name of the new file that will be created, with the new elements in the
% specified (approximate) proportions:
new_filename = 'graphene_modified.txt';
% elements to replace the "C"s with:
elements = ["Fe","Al","Ni"];
% (approximate) proportion of each element to use:
proportions = [40,30,30];
% show the original file's contents, for reference:
dbtype(filename)
1 168 2 generated by VMD 3 C 0.000000 0.000000 0.000000 4 C -0.866025 0.500000 0.000000 5 C -0.866025 1.500000 0.000000 6 C 0.000000 2.000000 0.000000 7 C 1.732051 0.000000 0.000000 8 C 0.866025 0.500000 0.000000 9 C 0.866025 1.500000 0.000000 10 C 1.732051 2.000000 0.000000 11 C 3.464102 0.000000 0.000000 12 C 2.598076 0.500000 0.000000 13 C 2.598076 1.500000 0.000000 14 C 3.464102 2.000000 0.000000 15 C 5.196152 0.000000 0.000000 16 C 4.330127 0.500000 0.000000 17 C 4.330127 1.500000 0.000000 18 C 5.196152 2.000000 0.000000 19 C 6.928203 0.000000 0.000000 20 C 6.062178 0.500000 0.000000 21 C 6.062178 1.500000 0.000000 22 C 6.928203 2.000000 0.000000 23 C 8.660254 0.000000 0.000000 24 C 7.794229 0.500000 0.000000 25 C 7.794229 1.500000 0.000000 26 C 8.660254 2.000000 0.000000 27 C 0.000000 3.000000 0.000000 28 C -0.866025 3.500000 0.000000 29 C -0.866025 4.500000 0.000000 30 C 0.000000 5.000000 0.000000 31 C 1.732051 3.000000 0.000000 32 C 0.866025 3.500000 0.000000 33 C 0.866025 4.500000 0.000000 34 C 1.732051 5.000000 0.000000 35 C 3.464102 3.000000 0.000000 36 C 2.598076 3.500000 0.000000 37 C 2.598076 4.500000 0.000000 38 C 3.464102 5.000000 0.000000 39 C 5.196152 3.000000 0.000000 40 C 4.330127 3.500000 0.000000 41 C 4.330127 4.500000 0.000000 42 C 5.196152 5.000000 0.000000 43 C 6.928203 3.000000 0.000000 44 C 6.062178 3.500000 0.000000 45 C 6.062178 4.500000 0.000000 46 C 6.928203 5.000000 0.000000 47 C 8.660254 3.000000 0.000000 48 C 7.794229 3.500000 0.000000 49 C 7.794229 4.500000 0.000000 50 C 8.660254 5.000000 0.000000 51 C 0.000000 6.000000 0.000000 52 C -0.866025 6.500000 0.000000 53 C -0.866025 7.500000 0.000000 54 C 0.000000 8.000000 0.000000 55 C 1.732051 6.000000 0.000000 56 C 0.866025 6.500000 0.000000 57 C 0.866025 7.500000 0.000000 58 C 1.732051 8.000000 0.000000 59 C 3.464102 6.000000 0.000000 60 C 2.598076 6.500000 0.000000 61 C 2.598076 7.500000 0.000000 62 C 3.464102 8.000000 0.000000 63 C 5.196152 6.000000 0.000000 64 C 4.330127 6.500000 0.000000 65 C 4.330127 7.500000 0.000000 66 C 5.196152 8.000000 0.000000 67 C 6.928203 6.000000 0.000000 68 C 6.062178 6.500000 0.000000 69 C 6.062178 7.500000 0.000000 70 C 6.928203 8.000000 0.000000 71 C 8.660254 6.000000 0.000000 72 C 7.794229 6.500000 0.000000 73 C 7.794229 7.500000 0.000000 74 C 8.660254 8.000000 0.000000 75 C 0.000000 9.000000 0.000000 76 C -0.866025 9.500000 0.000000 77 C -0.866025 10.500000 0.000000 78 C 0.000000 11.000000 0.000000 79 C 1.732051 9.000000 0.000000 80 C 0.866025 9.500000 0.000000 81 C 0.866025 10.500000 0.000000 82 C 1.732051 11.000000 0.000000 83 C 3.464102 9.000000 0.000000 84 C 2.598076 9.500000 0.000000 85 C 2.598076 10.500000 0.000000 86 C 3.464102 11.000000 0.000000 87 C 5.196152 9.000000 0.000000 88 C 4.330127 9.500000 0.000000 89 C 4.330127 10.500000 0.000000 90 C 5.196152 11.000000 0.000000 91 C 6.928203 9.000000 0.000000 92 C 6.062178 9.500000 0.000000 93 C 6.062178 10.500000 0.000000 94 C 6.928203 11.000000 0.000000 95 C 8.660254 9.000000 0.000000 96 C 7.794229 9.500000 0.000000 97 C 7.794229 10.500000 0.000000 98 C 8.660254 11.000000 0.000000 99 C 0.000000 12.000000 0.000000 100 C -0.866025 12.500000 0.000000 101 C -0.866025 13.500000 0.000000 102 C 0.000000 14.000000 0.000000 103 C 1.732051 12.000000 0.000000 104 C 0.866025 12.500000 0.000000 105 C 0.866025 13.500000 0.000000 106 C 1.732051 14.000000 0.000000 107 C 3.464102 12.000000 0.000000 108 C 2.598076 12.500000 0.000000 109 C 2.598076 13.500000 0.000000 110 C 3.464102 14.000000 0.000000 111 C 5.196152 12.000000 0.000000 112 C 4.330127 12.500000 0.000000 113 C 4.330127 13.500000 0.000000 114 C 5.196152 14.000000 0.000000 115 C 6.928203 12.000000 0.000000 116 C 6.062178 12.500000 0.000000 117 C 6.062178 13.500000 0.000000 118 C 6.928203 14.000000 0.000000 119 C 8.660254 12.000000 0.000000 120 C 7.794229 12.500000 0.000000 121 C 7.794229 13.500000 0.000000 122 C 8.660254 14.000000 0.000000 123 C 0.000000 15.000000 0.000000 124 C -0.866025 15.500000 0.000000 125 C -0.866025 16.500000 0.000000 126 C 0.000000 17.000000 0.000000 127 C 1.732051 15.000000 0.000000 128 C 0.866025 15.500000 0.000000 129 C 0.866025 16.500000 0.000000 130 C 1.732051 17.000000 0.000000 131 C 3.464102 15.000000 0.000000 132 C 2.598076 15.500000 0.000000 133 C 2.598076 16.500000 0.000000 134 C 3.464102 17.000000 0.000000 135 C 5.196152 15.000000 0.000000 136 C 4.330127 15.500000 0.000000 137 C 4.330127 16.500000 0.000000 138 C 5.196152 17.000000 0.000000 139 C 6.928203 15.000000 0.000000 140 C 6.062178 15.500000 0.000000 141 C 6.062178 16.500000 0.000000 142 C 6.928203 17.000000 0.000000 143 C 8.660254 15.000000 0.000000 144 C 7.794229 15.500000 0.000000 145 C 7.794229 16.500000 0.000000 146 C 8.660254 17.000000 0.000000 147 C 0.000000 18.000000 0.000000 148 C -0.866025 18.500000 0.000000 149 C -0.866025 19.500000 0.000000 150 C 0.000000 20.000000 0.000000 151 C 1.732051 18.000000 0.000000 152 C 0.866025 18.500000 0.000000 153 C 0.866025 19.500000 0.000000 154 C 1.732051 20.000000 0.000000 155 C 3.464102 18.000000 0.000000 156 C 2.598076 18.500000 0.000000 157 C 2.598076 19.500000 0.000000 158 C 3.464102 20.000000 0.000000 159 C 5.196152 18.000000 0.000000 160 C 4.330127 18.500000 0.000000 161 C 4.330127 19.500000 0.000000 162 C 5.196152 20.000000 0.000000 163 C 6.928203 18.000000 0.000000 164 C 6.062178 18.500000 0.000000 165 C 6.062178 19.500000 0.000000 166 C 6.928203 20.000000 0.000000 167 C 8.660254 18.000000 0.000000 168 C 7.794229 18.500000 0.000000 169 C 7.794229 19.500000 0.000000 170 C 8.660254 20.000000 0.000000
% read the file into string array L, each element of which is a line of
% text from the file:
L = readlines(filename);
% fidx: indices of the lines that start with " C":
fidx = find(startsWith(L," C"));
% total number of lines that will be altered:
N_lines = numel(fidx);
% number of lines that will be altered, for each element:
N = round(proportions/sum(proportions)*N_lines);
% enforce that sum(N) == N_lines:
N(end) = N_lines-sum(N(1:end-1));
% permute fidx to get a random replacement order:
fidx = fidx(randperm(N_lines));
% get the start and end index (in fidx) for each element:
temp = cumsum([0 N]);
s_idx = temp(1:end-1)+1;
e_idx = temp(2:end);
% loop over elements and replace the "C"s:
for ii = 1:numel(elements)
% indices of the random lines of text that will be changed to
% element(ii):
idx = fidx(s_idx(ii):e_idx(ii));
% replace "C" with element(ii) in those lines:
L(idx) = replace(L(idx),"C",elements(ii));
end
% write the result to a new file:
writelines(L,new_filename)
% verification: check the new file's contents:
dbtype(new_filename)
1 168 2 generated by VMD 3 Ni 0.000000 0.000000 0.000000 4 Al -0.866025 0.500000 0.000000 5 Al -0.866025 1.500000 0.000000 6 Al 0.000000 2.000000 0.000000 7 Ni 1.732051 0.000000 0.000000 8 Ni 0.866025 0.500000 0.000000 9 Al 0.866025 1.500000 0.000000 10 Fe 1.732051 2.000000 0.000000 11 Fe 3.464102 0.000000 0.000000 12 Fe 2.598076 0.500000 0.000000 13 Ni 2.598076 1.500000 0.000000 14 Al 3.464102 2.000000 0.000000 15 Ni 5.196152 0.000000 0.000000 16 Fe 4.330127 0.500000 0.000000 17 Fe 4.330127 1.500000 0.000000 18 Fe 5.196152 2.000000 0.000000 19 Fe 6.928203 0.000000 0.000000 20 Al 6.062178 0.500000 0.000000 21 Fe 6.062178 1.500000 0.000000 22 Ni 6.928203 2.000000 0.000000 23 Ni 8.660254 0.000000 0.000000 24 Al 7.794229 0.500000 0.000000 25 Ni 7.794229 1.500000 0.000000 26 Fe 8.660254 2.000000 0.000000 27 Al 0.000000 3.000000 0.000000 28 Ni -0.866025 3.500000 0.000000 29 Fe -0.866025 4.500000 0.000000 30 Fe 0.000000 5.000000 0.000000 31 Fe 1.732051 3.000000 0.000000 32 Al 0.866025 3.500000 0.000000 33 Fe 0.866025 4.500000 0.000000 34 Fe 1.732051 5.000000 0.000000 35 Fe 3.464102 3.000000 0.000000 36 Fe 2.598076 3.500000 0.000000 37 Ni 2.598076 4.500000 0.000000 38 Al 3.464102 5.000000 0.000000 39 Al 5.196152 3.000000 0.000000 40 Fe 4.330127 3.500000 0.000000 41 Al 4.330127 4.500000 0.000000 42 Ni 5.196152 5.000000 0.000000 43 Al 6.928203 3.000000 0.000000 44 Al 6.062178 3.500000 0.000000 45 Ni 6.062178 4.500000 0.000000 46 Fe 6.928203 5.000000 0.000000 47 Fe 8.660254 3.000000 0.000000 48 Al 7.794229 3.500000 0.000000 49 Fe 7.794229 4.500000 0.000000 50 Ni 8.660254 5.000000 0.000000 51 Ni 0.000000 6.000000 0.000000 52 Fe -0.866025 6.500000 0.000000 53 Fe -0.866025 7.500000 0.000000 54 Ni 0.000000 8.000000 0.000000 55 Ni 1.732051 6.000000 0.000000 56 Fe 0.866025 6.500000 0.000000 57 Ni 0.866025 7.500000 0.000000 58 Fe 1.732051 8.000000 0.000000 59 Fe 3.464102 6.000000 0.000000 60 Fe 2.598076 6.500000 0.000000 61 Fe 2.598076 7.500000 0.000000 62 Al 3.464102 8.000000 0.000000 63 Ni 5.196152 6.000000 0.000000 64 Fe 4.330127 6.500000 0.000000 65 Fe 4.330127 7.500000 0.000000 66 Ni 5.196152 8.000000 0.000000 67 Fe 6.928203 6.000000 0.000000 68 Fe 6.062178 6.500000 0.000000 69 Fe 6.062178 7.500000 0.000000 70 Al 6.928203 8.000000 0.000000 71 Ni 8.660254 6.000000 0.000000 72 Fe 7.794229 6.500000 0.000000 73 Al 7.794229 7.500000 0.000000 74 Ni 8.660254 8.000000 0.000000 75 Fe 0.000000 9.000000 0.000000 76 Ni -0.866025 9.500000 0.000000 77 Al -0.866025 10.500000 0.000000 78 Al 0.000000 11.000000 0.000000 79 Al 1.732051 9.000000 0.000000 80 Al 0.866025 9.500000 0.000000 81 Ni 0.866025 10.500000 0.000000 82 Ni 1.732051 11.000000 0.000000 83 Al 3.464102 9.000000 0.000000 84 Ni 2.598076 9.500000 0.000000 85 Fe 2.598076 10.500000 0.000000 86 Ni 3.464102 11.000000 0.000000 87 Al 5.196152 9.000000 0.000000 88 Fe 4.330127 9.500000 0.000000 89 Fe 4.330127 10.500000 0.000000 90 Ni 5.196152 11.000000 0.000000 91 Al 6.928203 9.000000 0.000000 92 Fe 6.062178 9.500000 0.000000 93 Fe 6.062178 10.500000 0.000000 94 Ni 6.928203 11.000000 0.000000 95 Fe 8.660254 9.000000 0.000000 96 Fe 7.794229 9.500000 0.000000 97 Fe 7.794229 10.500000 0.000000 98 Al 8.660254 11.000000 0.000000 99 Ni 0.000000 12.000000 0.000000 100 Ni -0.866025 12.500000 0.000000 101 Al -0.866025 13.500000 0.000000 102 Fe 0.000000 14.000000 0.000000 103 Fe 1.732051 12.000000 0.000000 104 Al 0.866025 12.500000 0.000000 105 Al 0.866025 13.500000 0.000000 106 Al 1.732051 14.000000 0.000000 107 Ni 3.464102 12.000000 0.000000 108 Ni 2.598076 12.500000 0.000000 109 Al 2.598076 13.500000 0.000000 110 Fe 3.464102 14.000000 0.000000 111 Fe 5.196152 12.000000 0.000000 112 Al 4.330127 12.500000 0.000000 113 Ni 4.330127 13.500000 0.000000 114 Al 5.196152 14.000000 0.000000 115 Ni 6.928203 12.000000 0.000000 116 Al 6.062178 12.500000 0.000000 117 Fe 6.062178 13.500000 0.000000 118 Ni 6.928203 14.000000 0.000000 119 Al 8.660254 12.000000 0.000000 120 Fe 7.794229 12.500000 0.000000 121 Ni 7.794229 13.500000 0.000000 122 Fe 8.660254 14.000000 0.000000 123 Fe 0.000000 15.000000 0.000000 124 Al -0.866025 15.500000 0.000000 125 Al -0.866025 16.500000 0.000000 126 Al 0.000000 17.000000 0.000000 127 Ni 1.732051 15.000000 0.000000 128 Fe 0.866025 15.500000 0.000000 129 Al 0.866025 16.500000 0.000000 130 Al 1.732051 17.000000 0.000000 131 Al 3.464102 15.000000 0.000000 132 Fe 2.598076 15.500000 0.000000 133 Al 2.598076 16.500000 0.000000 134 Fe 3.464102 17.000000 0.000000 135 Al 5.196152 15.000000 0.000000 136 Fe 4.330127 15.500000 0.000000 137 Al 4.330127 16.500000 0.000000 138 Al 5.196152 17.000000 0.000000 139 Ni 6.928203 15.000000 0.000000 140 Al 6.062178 15.500000 0.000000 141 Fe 6.062178 16.500000 0.000000 142 Fe 6.928203 17.000000 0.000000 143 Fe 8.660254 15.000000 0.000000 144 Ni 7.794229 15.500000 0.000000 145 Fe 7.794229 16.500000 0.000000 146 Fe 8.660254 17.000000 0.000000 147 Ni 0.000000 18.000000 0.000000 148 Ni -0.866025 18.500000 0.000000 149 Ni -0.866025 19.500000 0.000000 150 Ni 0.000000 20.000000 0.000000 151 Fe 1.732051 18.000000 0.000000 152 Ni 0.866025 18.500000 0.000000 153 Ni 0.866025 19.500000 0.000000 154 Fe 1.732051 20.000000 0.000000 155 Ni 3.464102 18.000000 0.000000 156 Fe 2.598076 18.500000 0.000000 157 Al 2.598076 19.500000 0.000000 158 Fe 3.464102 20.000000 0.000000 159 Al 5.196152 18.000000 0.000000 160 Al 4.330127 18.500000 0.000000 161 Fe 4.330127 19.500000 0.000000 162 Ni 5.196152 20.000000 0.000000 163 Ni 6.928203 18.000000 0.000000 164 Fe 6.062178 18.500000 0.000000 165 Al 6.062178 19.500000 0.000000 166 Fe 6.928203 20.000000 0.000000 167 Ni 8.660254 18.000000 0.000000 168 Ni 7.794229 18.500000 0.000000 169 Ni 7.794229 19.500000 0.000000 170 Fe 8.660254 20.000000 0.000000
% verification: read the new file and verify that each element starts the
% expected number of lines:
new_L = readlines(new_filename);
for ii = 1:numel(elements)
assert(nnz(startsWith(new_L," "+elements(ii))) == N(ii), ...
sprintf('Unexpected number of "%s" lines',elements(ii)));
end

Community Treasure Hunt

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

Start Hunting!