Replacing elements of a text file
Ältere Kommentare anzeigen
Dear community, i already searched for my matter but i do not find any good reply. Btw, i have a text file "map.txt" composed by different letters in a 25x60 matrix.
And i need to rewrite this matrix in a new text file replacing all the letters different from "i" with an "e" (it's an academic exercise). I really have different difficulties loading the file.
My idea was using some code like this one:
filename='mappa.txt';
delimiterIn='';
headerlinesIn=25;
A=importdata(filename,delimiterIn,headerlinesIn);
[n,m]=size(A);
Aout=zeros(size(A));
for i=1:n
for j=1:m
if (A(i,j)==('I'))
Aout(i,j)=('X');
else
Aout(i,j)=('~');
end
end
end
disp=(Aout)
but it obviously doesn't work. Any suggestion? Kindly, A.
2 Kommentare
dpb
am 5 Okt. 2015
importdata is for numeric, not character data.
Would need sample of the file to know precisely the input, but to do a simple character substitution, look at
doc strrep
Alberto Rizzo
am 5 Okt. 2015
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Matrix Indexing 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!