How to filter and delete text from a .m file?

조회 수: 11 (최근 30일)
Rodriguez Pham
Rodriguez Pham 2016년 6월 22일
답변: Shameer Parmar 2016년 6월 23일
so im creating a script that read another script and filter out and delete variables that I don't want. How should I go about this? I'm assuming using strfind and then strrep?
Scenario: I have a script lets call it A with a bunch of variables and I am working on script B that will read from an excel file and eval those paramteres. I want use script B to edit script A and removed the variables that are repeated from the excel spread sheet.

답변 (1개)

Shameer Parmar
Shameer Parmar 2016년 6월 23일
Hi Rodriguez,
To read your A.m file, you can use this..
Data = textread('A.m', '%s', 'delimiter', '');
All data of file A.m will be stored into variable ' Data'
you can read and edit the data in variable ' Data' line by line as follows:
for i=1:length(Data)
lineData = Data{i};
% add your logic to remove / replace the data
lineData = strrep(lineData,'ABC','XYZ'); % replace variable 'ABC' with 'XYZ'
newData{i} = lineData;
end
then you can write the newData into A.m file as follows:
fid = fopen('A.m','w');
for i=1:length(newData)
fprintf(fid,'%s\n',newData{i});
end
fclose(fid);

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by