Cell array Filtering when using readlines
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jason
el 29 de Abr. de 2025
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 comentarios
Respuesta aceptada
Stephen23
el 29 de Abr. de 2025
Editada: Stephen23
el 29 de Abr. de 2025
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 comentarios
Stephen23
el 29 de Abr. de 2025
"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'))
Más respuestas (1)
Star Strider
el 29 de Abr. de 2025
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
.
2 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!