Eliminate string from vector

조회 수: 3 (최근 30일)
Giacomo Abrardo
Giacomo Abrardo 2021년 6월 18일
댓글: Giacomo Abrardo 2021년 6월 19일
Hi, i created a vector containing 963 nc file. The problem is that some of them are the same but from two different version and i want to delete the previous version. For example, how can i eliminate row 844, 846 and 848 from my vector? Thanks all

채택된 답변

Image Analyst
Image Analyst 2021년 6월 19일
You say "For example from c_gls to S1CSAR" so basically up until the last underline. Don't include _V and anything after that. This will do it:
% Create sample data
ca = {...
'c_gls_SSM1km_202007310000_CEURO_S1CSAR_V1.1.1.NC';...
'c_gls_SSM1km_202007310000_CEURO_S1CSAR_V1.1.2.NC';...
'c_gls_SSM1km_202008040000_CEURO_S1CSAR_V1.1.1.NC';...
'c_gls_SSM1km_202008040000_CEURO_S1CSAR_V1.1.2.NC';...
'c_gls_SSM1km_202008050000_CEURO_S1CSAR_V1.1.1.NC';...
'c_gls_SSM1km_202008050000_CEURO_S1CSAR_V1.1.2.NC'}
% Loop over each cell replacing it with the contents
% but only until the last underline.
for k = 1 : length(ca)
underlineIndexes = find(ca{k} == '_');
% Take up until the last underline
ca{k} = ca{k}(1:underlineIndexes(end) - 1);
end
ca = unique(ca) % Get unique and show results in command window.
You get
ca =
3×1 cell array
{'c_gls_SSM1km_202007310000_CEURO_S1CSAR'}
{'c_gls_SSM1km_202008040000_CEURO_S1CSAR'}
{'c_gls_SSM1km_202008050000_CEURO_S1CSAR'}
Is that what you want?
If you know that the last underline is always in the same location, you could simplify it to be
for k = 1 : length(ca)
ca{k} = ca{k}(1:38); % Extract the first 38 characters of the kth cell.
end
OK, 3 lines instead of 1 for the regexp() way, but you might find this more intuitive and less cryptic.
  댓글 수: 1
Giacomo Abrardo
Giacomo Abrardo 2021년 6월 19일
thank you so much. that's work

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

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 6월 18일
Did you try the unique() function? It has lots of options so be sure you understand which options to use.
If you need more help, attach your cell array in a .mat file with the paperclip icon.
  댓글 수: 1
Giacomo Abrardo
Giacomo Abrardo 2021년 6월 18일
It could be the solution, but the name is not exactly the same cause at the end i have v1.1.1 and v1.1.2. There is a way to consider the strings without the last part of the letters? For example from c_gls to S1CSAR. Thank you very much

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by