arraysmatlabdata-structuressaveeeglab

How to save fields from a data structure to filename field in Matlab?


In Matlab I have a structural array that is as follows.

We basically have a number of datasets and each one has a name. For each dataset there is a certain number of data points, that are data that has been recorded from electrical activity in the brain, from presentation of a stimulus, over a certain number of seconds.

For each dataset, there was a recording of 2 seconds before the stimulus was presented and a recording of 3 seconds after the stimulus was presented. So I actually want to chop my data into 2, with data points associated to "pre" and data points associated with "post". This is quite simple to do using a for loop and I have done it and have now 2 additional fields associated with each data set.

FYI

ALLEEG(data_set).data ----- > this field has the original unchopped data
ALLEEG(data_set).data_pre ----- > this field has the "pre" data
ALLEEG(data_set).data_post ---- > this field has the "post" data 
ALLEEG(data_set).filename ---- > this field has the filename  

Now I wish to take the original filename of each dataset for e.g. if one of them was called

1234L01.set 

and had a field labelled data containing the full data recording of (non-chopped into "pre" and "post"), I want to save the "pre" and "post" fields that I created such that I have 2 new datasets

1234L01_pre.set and 1234L01_post.set 

and the data field in each of these is the "pre" and the "post" respectively and any other fields associated with the dataset are maintained.

I am a bit confused on how to do it because I don't understand how to take the original filename and modify it and I have lots of datasets so I don't want to do it all by hand.

Could anybody help with this please?


Solution

  • Something like:

    [p,f,e] = fileparts ( ALLEEG(data_set).filename );
    newFilename = sprintf ( '%s_pre.%s', f, e )
    pre = ALLEEG(data_set).pre;
    save ( newFilename, 'pre' );
    
    newFilename = sprintf ( '%s_post.%s', f, e )
    post = ALLEEG(data_set).post;
    save ( newFilename, 'post' );