% structuredemo is a script showing the usage of structures with a dummy
% example for the Matlab for PhD studen course.
% This script is part of the Matlab course for PhD Students 2010-2011:
% http://www.sinnesphysiologie.uni-oldenburg.de/50246.html
% This version was written by Jutta Kretzberg, 23.01.2011

%% make sure variable names are not in use

clear

%% generate dummy data
make_exampledata; 					% generates a random sparse matrix.
                                    % For real data you probably would 
									% load a file instead...
[r,c]=size(exampledata);            % dimensions of data set                                    
                                    
%% define a structure  

% Way1: using command struct 
stimulus=struct('binduration',0.001,'stimname','flashes1')
% creates a structure stimulus with fields binduration and stimname which
% are filled with the corresponding values.
% binduration: binduration in s
% stimname: name of stimulus file

% Way2: generate a structure by defining fields
spdata.data=exampledata;                %sparse matrix of spikes
spdata.trials=r;                        %number of trials
spdata.length=c*stimulus.binduration;   %length of trial in s
spdata.stimulus=stimulus               %structures can contain structures

input('Pause 1: press Return to continue');

%% access individual field contents

total_length=spdata.length*spdata.trials % use two fields to calculate value

datapiece=spdata.data(1,100:110)         % extract little piece of data
f_datapiece=full(datapiece)              % display data piece also in full
                                         % matrix notation

input('Pause 2: press Return to continue');

%% add second dimension

make_exampledata;				%generate new dummy data for 
								%second electrode
spdata(2)=spdata;				%duplicate all content	
spdata(2).data=exampledata;		%replace spike data by newly
								%generated dummy data
 
spdata                                  %display structure

input('Pause 3: press Return to continue');

%% add new field to structure

spdata(1).electrode=7			%include electrode number and display

spdata.electrode                %display electrodes

input('Pause 4: press Return to continue');

spdata(2).electrode=19			%include electrode number also in second 
                                %dimension and display
                                
spdata.electrode                %display electrodes                                
                                
input('Pause 5: press Return to continue');


%% access contents of more than one individual field 

L=length(spdata)                %determine how many items are in structure 

electrodes_all(1:L)=spdata(:).electrode     %extract all electrode numbers

% extract little piece of data for all electrodes into cell array
for ind=1:L
    datapiece_all{ind}=spdata(ind).data(1,100:110)  
end

%Alternative representation of little piece of data as 3-dimensional full matrix:                                                     
for ind=1:L                                                     
    datapiece_all_full(:,:,ind)=full(spdata(ind).data(1,100:110)) 
end

input('Pause 6: press Return to continue');

%% create an alternative representation of the data set without duplicating
% identical data by using a cell array for the data:

spdata_2=spdata(1);           %copy all fields of first dimension into 
                                %new structure
                                
clear spdata_2.data;          %clear data field of new structure to make space
                              %for new representation.
                              
celldata{1}=spdata(1).data;    %generate cell array for data
celldata{2}=spdata(2).data;
                                
spdata_2.data=celldata;        %include cell data into structure

spdata_2.electrode(2)=spdata(2).electrode;  %add information about second electrode number

spdata_2                      %display

datapiece2=spdata_2.data{1}(1,100:110) %example to extract piece of data

datapiece2_full=full(datapiece2)



                                     
                                     