MATLAB: Problem with identification of strings

dirstringsuigetdir

Good Mornig,
could you find a solution for this: if I input:
files = dir(fullfile(open_folder, '*140821*.txt'));
it finds a structure array, anyway, if I try with:
b=strcat('*','140821','*')
files = dir(fullfile(open_folder, 'b.txt'));
It doesn't find any.
I would like to name a 'b' and than find the related file in the directory but that doesn't seem the right way!!
Could you help me?

Best Answer

files = dir(fullfile(open_folder, [b '.txt']));
or
files = dir(fullfile(open_folder, strcat(b, '.txt')));
or
files = dir(fullfile(open_folder, sprintf('%s.txt', b)));
The last one is my personal preference, and I would build b the same way:
b = sprintf('*%d*', 140821);
Note that in 'b.txt' the b is just the character 'b', there's no reason matlab would interpret it as the variable b.