Replacing elements of a text file
이전 댓글 표시
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
dpb
2015년 10월 5일
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
2015년 10월 5일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!