필터 지우기
필터 지우기

compare particular strings in filenames array

조회 수: 7 (최근 30일)
yogesh kumkar
yogesh kumkar 2011년 8월 10일
Hello All, If i create a cell like vars = {'OKR_evapo','MDF_albedo', 'HDU_wind','GDf_temp','MPGF_humidity','MDF_pressure','YKK_temperature','XRYO_rain','MPIO_radiation'}
And I have files in dir D:\mywork\weather\ 'myd_11_MDF_albedo_a54576' 'mod_19_GDf_temp_vhk464567' 'mcd_13_MPGF_humidity.sdjfg590856' 'myd_11_MDF_pressure_46358'
and few files in dir D:\myworks\climate\ 'cyd_11_YKK_temperature_a54576' 'dod_13_XRYO_rain_vhk464567' 'ecd_11_MPIO_radiation.sdjfg590856'
This means I have total 7 files in dir D:\myworks\ but I have total 9 vars. this means the files 'hui_23_OKR_evapo_3576' and 'ghy_12_HDU_wind_243245' are missing.
All I want to get the vector/cell like files = [0 1 0 1 1 1 1 1 1 ] because, the files 1 and 3 are missing. if all files are missing, it should give me [ 0 0 0 0 0 0 0 0 0 ]. this is to keep the record of thousands of files.
any suggestions? thanks in advance. yk

답변 (3개)

Fangjun Jiang
Fangjun Jiang 2011년 8월 10일
So you want to compare the file names with some strings to see if they partially match. Use dir() to get all your file names from different folders.
vars = {'OKR_evapo','MDF_albedo','HDU_wind','GDf_temp','MPGF_humidity',...
'MDF_pressure','YKK_temperature','XRYO_rain','MPIO_radiation'};
files={'myd_11_MDF_albedo_a54576' 'mod_19_GDf_temp_vhk464567' ...
'mcd_13_MPGF_humidity.sdjfg590856' 'myd_11_MDF_pressure_46358' ...
'cyd_11_YKK_temperature_a54576' 'dod_13_XRYO_rain_vhk464567' ...
'ecd_11_MPIO_radiation.sdjfg590856'};
Index=false(size(vars));
for k=1:length(vars)
for j=1:length(files)
if strfind(files{j},vars{k})
Index(k)=true;
break;
end
end
end
  댓글 수: 2
Oleg Komarov
Oleg Komarov 2011년 8월 10일
k = strfind(cellstr, pattern)
No need for nested loop?
Fangjun Jiang
Fangjun Jiang 2011년 8월 10일
Oleg,I tried but couldn't make it work. It's not exactly finding the patterns. It wants a logical index on vars, not on files. Use the data in vars and files to try and you'll understand what I mean.

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


Oleg Komarov
Oleg Komarov 2011년 8월 10일

YOGESH
YOGESH 2011년 8월 15일
Thankx Fangjun Juang and Oleg. This code works fine when there is a single row of files. But I do have a cell of size 414*74. is there a way to process it in a loop? i mean i have files = cell(414,74). in the end I should be able to get a matrix of size 414*74 containing 1 and 0s.
cheers, yk
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2011년 8월 15일
What is the size of vars?
Fangjun Jiang
Fangjun Jiang 2011년 8월 15일
Wait a miniute! In your original example, vars is 9x1, files is 7x1 and you were expecting an Index of 9x1 so the Index is the same size as vars. Now you are saying you want an Index the same size as files?

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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by