Cell array Filtering when using readlines
6 次查看(过去 30 天)
显示 更早的评论
Hello, after I have read an array from a piece of equipment
writeline(s,command); pause(0.02);
data='N/A';
nb=s.NumBytesAvailable
data={}; ct=0;
while nb>0
ct=ct+1;
d = readline(s)
data{ct,1}=d;
end
data
I get this:
data =
884×1 cell array
{[" 190 190.03 190.06 190.09 190.12 190.15 190.18 190.21"]}
{[" 190.24 190.27 190.3 190.33 190.36 -999 -999 -999"]}
{[" -999 -999 -999 -999 -999 -999 -999 -999"]}
{[" -999 -999 -999 -999 -999 -999 -999 -999"]}
{[" -999 -999 -999 -999 -999 -999 -999 -999"]}
...
{[" 190.24 190.27 190.3 190.33 190.36 -999 -999 -999"]}
{[" -999 -999 -999 -999 -999 -999" ]}
...
{["-1.38866E-009 -999 -999 -999 -999 -999 -999" ]}
{[" -999 -999 -999" ]}
{[":"
I want to remove any line that doesn't have just -999 as all the values, so I tried modifing to this:
writeline(s,command); pause(0.02);
data='N/A';
nb=s.NumBytesAvailable
data={}; ct=0; idx=0;
while nb>0
ct=ct+1;
d = readline(s)
if d~=[" -999 -999 -999 -999 -999 -999 -999 -999"]
idx=idx+1;
data{idx,1}=d;
end
nb=s.NumBytesAvailable
end
data
and this nearly gives me what I want
data =
11×1 cell array
{[" 190 190.03 190.06 190.09 190.12 190.15 190.18 190.21"]}
{[" 190.24 190.27 190.3 190.33 190.36 -999 -999 -999"]}
{[" -999 -999 -999 -999 -999 -999" ]}
{[" -0.999999 -1 -1 -1 -1.00001 -1.00001 -1.00001 -1.00001"]}
{[" -1.00002 -1.00002 -1.00002 -1.00003 -1.00003 -999 -999 -999"]}
{[" -999 -999 -999 -999 -999 -999" ]}
{["-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009" ]}
{["-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009" ]}
{["-1.38866E-009 -999 -999 -999 -999 -999 -999" ]}
{[" -999 -999 -999" ]}
{[":" ]}
How can I also get rid of these line entries:
{[" -999 -999 -999 -999 -999 -999" ]}
{[" -999 -999 -999" ]}
{[":" ]}
Thanks for any help
0 个评论
采纳的回答
Stephen23
2025-4-29
编辑:Stephen23
2025-4-29
Do not store lots of scalar strings in a cell array, doing so makes processing them harder:
S = [" 190 190.03 190.06 190.09 190.12 190.15 190.18 190.21"; " 190.24 190.27 190.3 190.33 190.36 -999 -999 -999"; " -999 -999 -999 -999 -999 -999"; " -0.999999 -1 -1 -1 -1.00001 -1.00001 -1.00001 -1.00001"; " -1.00002 -1.00002 -1.00002 -1.00003 -1.00003 -999 -999 -999"; " -999 -999 -999 -999 -999 -999"; "-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009"; "-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009"; "-1.38866E-009 -999 -999 -999 -999 -999 -999"; " -999 -999 -999"; ":"]
X = cellfun(@isempty,regexp(S,'^(\s+-999)+|:$','once'));
Z = S(X)
4 个评论
Stephen23
2025-4-29
"I've also just realised that your S is different to my 'd'. I only get one line at a time"
I assumed that you wanted to filter the entire array after the loop. You can also apply the REGEXP to one single line of text, in which case you might want to use e.g. IF rather than indexing:
d = " 190 190.03 190.06 190.09 190.12 190.15 190.18 190.21";
isempty(regexp(d,'^(\s+-999)+|:$','once'))
d = " -999 -999 -999 -999 -999 -999";
isempty(regexp(d,'^(\s+-999)+|:$','once'))
更多回答(1 个)
Star Strider
2025-4-29
Try something like this —
% data =
% 884×1 cell array
data = { ...
{[" 190 190.03 190.06 190.09 190.12 190.15 190.18 190.21"]}
{[" 190.24 190.27 190.3 190.33 190.36 -999 -999 -999"]}
{[" -999 -999 -999 -999 -999 -999" ]}
{[" -0.999999 -1 -1 -1 -1.00001 -1.00001 -1.00001 -1.00001"]}
{[" -1.00002 -1.00002 -1.00002 -1.00003 -1.00003 -999 -999 -999"]}
{[" -999 -999 -999 -999 -999 -999" ]}
{["-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009" ]}
{["-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009" ]}
{["-1.38866E-009 -999 -999 -999 -999 -999 -999" ]}
{[" -999 -999 -999" ]}
{[":" ]}};
% Q = cellfun(@(x) x, data, Unif=0) % See What 'cellfun' Returns
% Q{1}
% Q{end}
db = cellfun(@(x)sscanf(x{:},"%f"), data, Unif=0); % Use 'sscanf' To Convert The String Values To Numeric
Lvc = cellfun(@(x) ~all(x == -999), db, Unif=0); % Test For 'all' Values To Be -999
Lv = cell2mat(Lvc); % Convert Cell Logical Array To Logical Array
data{Lv,:} % Return Edited Cell Array
.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!