compare particular strings in filenames array

7 次查看(过去 30 天)
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
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