how to remove values based on space delimiter
2 次查看(过去 30 天)
显示 更早的评论
the values present in my file is
f 1 2 3
f 1 2 3
f 1 2 3 4
f 2 3 4
f 4 5 6 7
i have to read every line and check for 4 values separated by space
ie f 1 2 3
if that line has 5 values the last value should get removed
ie
my output should be
f 1 2 3
f 1 2 3
f 1 2 3
f 2 3 4
f 4 5 6
does any built in is there ..pls help....thanks in advance
0 个评论
采纳的回答
Walter Roberson
2012-12-24
S = fileread('YourFileName.txt'); %read file
NewS = regexprep(S, '(?m)(?-s)^(\w+\s+\w+\s+\w+).*', '$1'); %do edit
fid = fopen('YourNewFile.txt, 'wt'); %write out new version
fwrite(fid, NewS);
fclose(fid);
3 个评论
Walter Roberson
2012-12-24
'(?m)(?-s)^(\w+\s+[0-9\\]+\s+[0-9\\]+).*'
This presumes that the first thing on the line will not be of the same form, and that the other values only have digits and backslash.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!