# Matlab - "cell contents reference from a non-cell array object"



## Niushastik (Feb 11, 2011)

Dear all,
I get the following error when running the script pasted below. 
I'd be most grateful if someone could let me know what the problem is. The script runs for a bit and then gives me this error. 
Many thanks in advance,
Anya



??? Cell contents reference from a non-cell array object.

Error in ==> script_nott_xca at 15
sj = data{i}(j)



%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Run PCA and ICA

function script_nott_xca()

pathNott = '/Users/elastyctmp/Desktop/EEGlab data analysis/Nott analysis/';
pathNottXCA = '/Users/elastyctmp/Desktop/EEGlab data analysis/Nott analysis/XCA/';

data = {[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 16], [1, 2, 3, 4, 6, 8, 9, 12, 13, 14, 16, 17], [1, 2, 3, 4, 5, 6, 7, 9, 12, 13, 15, 16, 17, 18]};

for i = 1:length(data),
for j = 1:length(data{i}),

list = i
sj = data{i}(j) 


EEG = pop_loadset('filename', sprintf('L%d_P%d.set', list, sj), 'filepath', pathNott);
EEG = eeg_checkset( EEG );

% PCA runs first to identify number of components that explain 99% of data
% (variance_portion). Then use that number of components in ICA.
variance_portion = 0.99; % for pca reduction
data = reshape(double(EEG.data),EEG.nbchan,[]);


num_ica = max(min(35,get_data_num_pca(data,variance_portion)),20); %get_data_num_pca is function - make sure it's in the right directory 
% get_data_num_pca identifies up to 35 components - any more than that
% will not explain significantly more data anyway. 
[weights,sphere] = runica(data,'pca',num_ica); % Run ICA using identified number of components
save(sprintf('%sL%d_P%d_ICA.mat', pathNottXCA, list, sj),'weights','sphere','-mat','-V6') %save weights and spheres matrices
pop_saveset(EEG, sprintf('L%d_P%d_ICA.set', list, sj) ,pathNottXCA); %resave file
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Plot ICA components and identify eyeblinks and lateral eye movements

% This will plot original data, component maps, component waveforms, and
% cleaned data, and will give a pop-up window asking which components to
% reject. Enter each component's number (1-35) and the cleaned data graph
% will show what happens when those components are subtracted from the
% data. The pop-up window keeps coming up until the same vector of
% components to remove is entered twice.


for i = 1:length(data),
for j = 1:length(data{i}),

list = i
sj = data{i}(j) 

EEG = pop_loadset('filename', sprintf('L%d_P%d_ICA.set', list, sj), 'filepath', pathNottXCA);
EEG = eeg_checkset( EEG );

load(sprintf('%sL%d_P%d_ICA.mat', pathNottXCA, list, sj));

EEG.icasphere = sphere;
EEG.icaweights = weights;
EEG.icawinv = pinv(weights * sphere); % Rename ICA weights and do some data stuff
EEG = eeg_checkset(EEG);
mix_matrix = weights * sphere; 
winv = pinv(mix_matrix);

data = double(reshape(EEG.data,EEG.nbchan,[]));
icaact = mix_matrix * data;
data = reshape(data,EEG.nbchan,EEG.pnts,[]);
icaact = reshape(icaact,size(icaact,1),EEG.pnts,[]);
EEG.icaact = icaact;

ncomps = [];
rejcomps = []; 

% plot component data 
% spacing changed to '15' to display scale at higher amplitude
eegplot(EEG.icaact,'srate',EEG.srate, 'spacing',15,'limits',[-500 1000],'winlength',2,'ploteventdur','off','submean','off','events',EEG.event);
% plot original electrode data
eegplot(EEG.data,'srate',EEG.srate,'spacing',15,'limits',[-500 1000],'winlength',2,'ploteventdur','off','submean','off','events',EEG.event,'title',['orig']);

% user selects components for rejection

% display topoplots of all components in one figure
h=figure;
dim = ceil(sqrt(size(mix_matrix,1)));
for comp=1:size(mix_matrix,1)
subplot(5,7,comp);
topoplot(winv,comp),EEG.chanlocs,'electrodes','off','numcontour',0);
% pop_topoplot(winv,comp),'typeplot',0,'items',comp) %how to get
% title for each component? gives error of 'referencing field of
% non-structure array in EEG.chanlocs'

% col = 'k'; if(ismember(comp,rejcomps)), col = 'r'; end;
% simil = round(similarity(comp) * 100)/100;
% simil_str = num2str(simil);
% title([' ' num2str(comp) ' (' simil_str([1:min(4,end)]) ')'],'color',col);
axcopy(gcf);
ncomps(1, comp) = comp;
end
set(h,'Position',[800 400 500 400],'color',[1 1 1]);
saveas(h, sprintf('%sL%d_P%d.pdf', pathNottXCA, list, sj), 'pdf');

% pop_prop(EEG,0,ncomps,[2 50]);
cleanEEG = pop_subcomp(EEG,rejcomps); %rejcomps is empty now, but need to start with empty dataset
cleanEEG = eeg_checkset(cleanEEG);

% plot clean data with specified components removed
eegplot(cleanEEG.data,'srate',EEG.srate,'spacing',15,'limits',[-500 1000],'winlength',2,'ploteventdur','off','submean','off','events',EEG.event,'title',['clean ' num2str(rejcomps)]);
done = 0;
while ~done
% ask user to enter comps corresponding to ocular artefacts
prompts={'Components to reject'}; % prompt to user
defaultanswers = {num2str(rejcomps)};
answers = inputdlg(prompts,'subj',1,defaultanswers); %'answers' store components identified by user

% if user presses Cancel then answers = {} and nothing gets saved
if ~isempty(answers{1})
newrejcomps = sort(unique(str2num(answers{1})));
if isempty(setdiff(newrejcomps,rejcomps)) & isempty(setdiff(rejcomps,newrejcomps))
done = 1;
else 
rejcomps = newrejcomps;
end
end

cleanEEG = pop_subcomp(EEG,rejcomps);
cleanEEG = eeg_checkset(cleanEEG); 

% display clean data
eegplot(cleanEEG.data,'srate',EEG.srate,'spacing',15,'limits',[-500 1000],'winlength',2,'ploteventdur','off','submean','off','events',EEG.event,'title',['clean ' num2str(rejcomps)]); 
%pop_prop(EEG,0,comp,[2 50]);
end

save(sprintf('%sL%d_P%d_rejcomps.mat', pathNottXCA, list, sj),'rejcomps','-mat'); %save data of which components were removed
pop_saveset(EEG, sprintf('L%d_P%d_ICA_cleaned.set', list, sj) ,pathNottXCA); %resave file

close all;

end
end


----------

