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

1 view (last 30 days)
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

Raheel Ejaz
Raheel Ejaz on 4 Mar 2021
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
Raheel Ejaz
Raheel Ejaz on 8 Mar 2021
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);
Raheel Ejaz
Raheel Ejaz on 8 Mar 2021
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)

Tags

Community Treasure Hunt

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

Start Hunting!