I want help to delete parameters from text file with matlab coding

I have requirement to delete some specific parameters from text file and generate new text file with changes. this should be done with matlab coding.
text file is attached herewith.
Following parameters should be deleted from text file:
  1. Filter_Reset
  2. GlobalDefault
  3. Enable
  4. High_Threshold
  5. Low_Threshold

 Accepted Answer

clear;clc;
fileID = fopen('try.txt','r');
S = textscan(fileID,'%s');
fclose(fileID);
S2=[S{:}];
param={'Filter_Reset','GlobalDefault','Enable','High_Threshold','Low_Threshold'};
param_i=zeros(1,length(param));
for i=1:length(param)
param_i(i)=find(~cellfun(@isempty,strfind(S2,param{1,i})));
S2(param_i(i))=[];
end
delete('try.txt')
fileID=fopen('try.txt','at');
for j=1:length(S2)
fprintf(fileID,'%s\n',S2{j,1});
end
fclose(fileID);

3 Comments

Thank you for your support! @Raheel Ejaz
Script is working.
One modification to this script -
this was just a try. but I want to remove long string of parameters from large data and everytime removed parameters will be different so code should be general so that I can replace these parameters easily. So what should be done for this?
One more que I have such as if that parameter has some value then what changes should made in code so that parameter with value should delete. (for eg. Enable = 1 )
clear;clc;
fileID = fopen('try.txt','r');
S = textscan(fileID,'%s');
fclose(fileID);
S2=[S{:}];
ter='y';
while strcmpi(ter,'y')
param={input('Parameter to delete: ','s')};
param_i=find(~cellfun(@isempty,strfind(S2,param{1})));
S2(param_i)=[];
ter=input('Do You want to delete more? Press Y to Continue and N to terminate: ','s');
end
delete('try.txt')
fileID=fopen('try.txt','at');
for j=1:length(S2)
fprintf(fileID,'%s\n',S2{j,1});
end
fclose(fileID);
You can have input from user in this code.... for second part if you want to delete the value of parameter only, You can get the index number of parameter from this line "param_i=find(~cellfun(@isempty,strfind(S2,param{1})));" then you can remove its value.
e.g(Enable = 1, 'If "Enable" is at index # 20 then "=" would be at index# 21 and "1" would be at index# 22' , then code next line become "S2(param_i+2)=[];") . It will delete only "1"
Hopefully I am able to solve the issue.

Sign in to comment.

More Answers (0)

Categories

Find more on Scripts in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!