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

1 次查看(过去 30 天)
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

采纳的回答

Raheel Ejaz
Raheel Ejaz 2021-3-4
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 个评论
Raheel Ejaz
Raheel Ejaz 2021-3-8
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 2021-3-8
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by