cell contents in a loop_fangjun's answer
Show older comments
Hello all, I have a vars = cell(1,9) like following, the code works fine when there is a single row.
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
How can I read all the rows in a loop if I have data/filenames in cell(10,9) instead of the single row. There are multiple rows in a cell of size 10*9. I should get a matrix of size 10*9 containing 1 and 0s.
2 Comments
Oleg Komarov
on 15 Aug 2011
Please format the code: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup#answer_18099
Fangjun Jiang
on 15 Aug 2011
Why do you ask the same question in two different places?
Accepted Answer
More Answers (3)
Oleg Komarov
on 15 Aug 2011
vars = {'ind' 'ger' 'us' 'swis' 'kor' 'ch'};
files = {'india' ,'germany','usa' ,'korea','','';...
'germany','usa' ,'china','' ,'','';...
'usa' ,'swiss' ,'china','' ,'',''};
szF = size(files);
Index = false(szF);
for n = 1:numel(vars)
[r,c] = find(~cellfun('isempty',strfind(files,vars{n})));
c(:) = n;
Index(sub2ind(szF,r,c)) = true;
end
Jan
on 15 Aug 2011
0 votes
Replace "for k=1:length(vars)" by "for k=1:numel(vars)".
YOGESH
on 15 Aug 2011
7 Comments
Fangjun Jiang
on 15 Aug 2011
Check again. The result from the code is correct!
Oleg Komarov
on 15 Aug 2011
No, he want to run strfind(vars,files).
Fangjun Jiang
on 15 Aug 2011
@Oleg, in your solution, you also used strfind(files,vars)?!
Oleg Komarov
on 15 Aug 2011
Yes because there's no way to find a longer string in a shorter. To emulate the effect of strfind(vars,files) I adjust the column position to n.
Fangjun Jiang
on 15 Aug 2011
No. The issue is not that! The issue is who the logical index should be corresponding to! From all his description and example, I thought the index should be corresponding to the long strings in the variable "files". For any one of the long strings in the variable "files", if any of the short strings is found, the corresponding logical index should be true!
But apparently NOT! What he wants is a logical index with the same number of rows as "files" but same columns as "vars".
Fangjun Jiang
on 15 Aug 2011
To be honest, the OP's description and examples are not clear or even mis-leading. Anyway, I added another version which created the results expected. It is a tedious for-loop with strfind(). That's all.
Oleg Komarov
on 15 Aug 2011
@Fangjun: well, you're rigth about the confusion. In fact my solution works for the specific example just because files is padded with empty strings.
I guess it won't be difficult to preallocate Index with m from files and n from vars.
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!