Using strepp on a textfile with replacement from array.
Ältere Kommentare anzeigen
EDIT due to comments (learning to ask better questions)
Inputs
1)I have an EXCEL array of dimensions 322x8 containing numeric data of a specific format (3.548E-07). e.g.
if true
H+ Ca Mg HCO3 Na K SO4 Cl
3.548E-07 1.183E-01 3.222E-02 1.560E-02 5.771E-01 0.000E+00 2.666E-02 8.113E-01
continued for 321 more rows
end
2)I have a text template file and contained within are 8 strings (%1 to %8) e.g.
if true
'GASES'
'*'
'SURFACE COMPLEXES'
'*' 'no mineral' 'xoh'
'SPECIES W/ Kd and DECAY decay constant(1/s)'
'*' 0.0 0.0 0.0
'EXCHANGEABLE CATIONS' 0
' master convention ex. coef.'
'*' 0 0 0.000e+000
'------------------------------------------------------------------------------'
'INITIAL AND BOUNDARY WATER TYPES'
1 1 !niwtype, nbwtype = number of initial and boundary waters
1 30.0 !iwtype initial, temp (C)
' icon guess ctot constrain' ! Modern seawater
'h2o' 1 1.000d+0 1.000d+0 ' ' 0 !
'h+' 3 %1 %1 ' ' 0 !pH=8.22
'ca+2' 1 %2 %2 ' ' 0
'mg+2' 1 %3 %3 ' ' 0
'hco3-' 1 %4 %4 ' ' 0
'na+' 1 %5 %5 ' ' 0
'k+' 1 %6 %6 ' ' 0
'so4-2' 1 %7 %7 ' ' 0
'cl-' 1 %8 %8 ' ' 0
'*' 0 0.0 0.0 ' ' 0
1 30.0 !iwtype boundary, temp (C)
end
Process and Output
1) I need to replace the 8 strings from the template file with the row data. I need to repeat this 322 times and save each newly created text file with exactly the same name to a directory.
Thoughts So far I have tried to use the' strrep' function failing miserably.
Please Help!!!!
3 Kommentare
We can help you achieve this. To do this you should explain the input and output requirements a bit better. We understand MATLAB quite well, so if you tell us a bit more about what you want to achieve, then we can help you with that. Showing us broken code is not so helpful, because it doesn't actually tell us what you want to happen (unless you want to fix that code, of course).
For a start: What is the size of V1 and of what data class is it? Can you please show us exactly the strings (or an example) from the text file? And finally give an example of how you want the output to look like (to be saved in the text file), or explain it a bit more.
EDIT: Thank you for editing your question, it is much nicer!
Hillaryfor2016
am 17 Feb. 2015
Bearbeitet: Hillaryfor2016
am 17 Feb. 2015
Akzeptierte Antwort
Weitere Antworten (1)
Here is a pseudocode outline of how you could do this:
data = readExcel;
temp = fileread(filename);
%
xpr = cellstr(reshape(sprintf('(?<=\\s)%%%d(?=\\s)',1:8),[],8).');
%
for k = 1:rows(data)
% extract the data for one instance:
vec = data(k,:);
% convert vec to cell of strings, if required.
% vec should have one string value per cell, size 1x8.
% replace the values in the template:
str = regexprep(temp, xpr, vec);
%
fid = fopen(filename);
fprintf(fid,str);
fclose(fid);
end
You can use regexprep to replace all of those strings simultaneously, without any loops. Because '%1' occurs in other places in the template, I only replace instances of ' %N ', ie with a space character on both sides. You can use fileread to get the whole template as one string.
2 Kommentare
Hillaryfor2016
am 17 Feb. 2015
Bearbeitet: Hillaryfor2016
am 17 Feb. 2015
Stephen23
am 17 Feb. 2015
Glad to help. You can always click on "Votes" :)
Kategorien
Mehr zu Spreadsheets finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!