Cell array Filtering when using readlines

조회 수: 6 (최근 30일)
Jason
Jason 2025년 4월 29일
편집: Stephen23 2025년 6월 2일
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

채택된 답변

Stephen23
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"; ":"]
S = 11x1 string 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" ":"
X = cellfun(@isempty,regexp(S,'^(\s+-999)+|:$','once'));
Z = S(X)
Z = 7x1 string 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" " -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" "-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"
  댓글 수: 4
Stephen23
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'))
ans = logical
1
d = " -999 -999 -999 -999 -999 -999";
isempty(regexp(d,'^(\s+-999)+|:$','once'))
ans = logical
0
Jason
Jason 2025년 4월 29일
Yes thats done it, thankyou!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Star Strider
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
ans = 1x1 cell array
{[" 190 190.03 190.06 190.09 190.12 190.15 190.18 190.21"]}
ans = 1x1 cell array
{[" 190.24 190.27 190.3 190.33 190.36 -999 -999 -999"]}
ans = 1x1 cell array
{[" -0.999999 -1 -1 -1 -1.00001 -1.00001 -1.00001 -1.00001"]}
ans = 1x1 cell array
{[" -1.00002 -1.00002 -1.00002 -1.00003 -1.00003 -999 -999 -999"]}
ans = 1x1 cell array
{["-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009"]}
ans = 1x1 cell array
{["-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009-1.38866E-009"]}
ans = 1x1 cell array
{["-1.38866E-009 -999 -999 -999 -999 -999 -999"]}
.
  댓글 수: 2
Jason
Jason 2025년 4월 29일
Thankyou
Star Strider
Star Strider 2025년 4월 29일
My pleasure!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by