After 17 comments it's time I add some working code. I still don't understand the intended stucture of the cell array, EEGData. The following code is my solution to OP's underlying problem as I understand it. My steps:
- download of Data1.01_pre.txt and creation of another five files by copy&rename.
- move of the six files to a separate folder, my_eeg_data. I think it helps to store experimental data in dedicated folders. However, the function, cssm, doesn't depend on a separate data folder.
- creation of the function, cssm (attached). I find it easier to develop and use functions compared to scripts. I understand that OP wish to use the "filename" as key to reference the data. (The names of the files are not valid Matlab variable names.)
- demo of the function, cssm
Demo:
>> eeg_data_pre = cssm( 'h:\m\cssm\my_eeg_data', 'Data*pre*.txt' )
>> eeg_data_post = cssm( 'h:\m\cssm\my_eeg_data', 'Data*post*.txt' )
>> eeg_data = cat( 1, eeg_data_pre, eeg_data_post );
>> keys(eeg_data)
ans =
Columns 1 through 3
'Data1.01_post.txt' 'Data1.01_pre.txt' 'Data1.02_post.txt'
Columns 4 through 6
'Data1.02_pre.txt' 'Data1.03_post.txt' 'Data1.03_pre.txt'
>> num = eeg_data( 'Data1.02_post.txt' );
>> whos num
Name Size Bytes Class Attributes
num 12x20 1920 double
 
cssm (I use body text to comment the code.)
The function comprises to parts: creation of a "list" of file names and a loop over all file names to read the data
function eeg_data = cssm( folder, glob )
dir is a robust way to retrieve the names of the files. It avoids the problem of reconstructing the names.
sad = dir( fullfile( folder, glob ) );
Possibly the "list" requires some manipulations. Here I sort it with respect to the number, which is between the dot and the underscore. However, with the leading zero as in this case it is not needed. And with the solution in this function it is not needed anyhow.
cac = regexp( {sad.name}, '(?<=\.)\d{1,2}(?=_)', 'match' );
num = str2double( [cac{:}] );
[~,ixs] = sort( num );
sad = sad( ixs );
len = length( sad );
The number of columns could have been retrieved from the text file.
Here I use containers.Map because it makes it possible to use the names of the files as key values. Had the names of the files been valid Matlab names I would have used a struct.
eeg_data = containers.Map( 'KeyType', 'char', 'ValueType', 'any' );
for jj = 1 : len
filespec = fullfile( folder, sad(jj).name );
[fid,msg] = fopen( filespec );
This test might not be justified since the names are created by dir
assert( not(fid==-1) ...
, 'MY:eeg_data:CannotOpenFile'...
, 'Failed to open "%s": "%s"' ...
, filespec, msg )
I read the data to a temporary variable. I use repmat to create the format string. It saves me from counting to twenty. The defaults takes care of the list delimiter.
buf = textscan( fid, repmat('%f', [1,nCol] )...
, 'CollectOutput' , true ...
, 'Headerlines' , 1 );
eeg_data( sad(jj).name ) = buf{:};
end
end
 
For me this was a worthwhile exercise with containers.Map. This is the first time I concatenate maps. Is that documented or does it go without saying?
Best Answer