Replacing elements of a text file
Afficher commentaires plus anciens
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 commentaires
dpb
le 5 Oct 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
le 5 Oct 2015
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Workspace Variables and MAT Files dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!