How to save variables in a repeating function, without overwriting the previously saved variable?

3 views (last 30 days)
Hi everyone,
We are two studenets working on a project where we need to create something called training templates for 4 targets on a screen (BCI if anybody knows what this is). Each template should have the dimensions 1 x 4 x "number of acquired samples" ( i.e. a 3D matrix). We have made a GUI where we first make a training template for target 1, then target 2, target 3 and finally target 4. We have made an if-condition where, depending on which target we are training, the required calculations are done for the specific target . At the end of the if-condition we append the created training template as a row to the 1x4xsamples-matrix. Our problem is that when we have created a training template for one of the targets, and continue to the next training session we end up overwriting our previously made training template.
So how do we save our results from the previosly run if-statement without overwriting it when we are running the if-statement for a new target??
Here's the relevant part of our script :
data.trainingtemplate = []; % Matrix containing training templates.
if get(handles.popupmenu_select_target,'Value')==2
data.trainingtemplate(1,1,:) = m;
data.templatesize(1) = length(m);
C = zeros(1,N-1);
for i=1:N-1
d1 = shiftdim(data.trainingtemplate(1,1,:))';
d2 = B(i,:);
C(i) = corr2(d1,d2);
end
r(1) = mean(C);
set(handles.text54,'String',int2str(r(1)*1000));
elseif get(handles.popupmenu_select_target,'Value')==3
data.trainingtemplate(1,2,:) = m;
data.templatesize(2) = length(m);
C = zeros(1,N-1);
for i=1:N-1
d1 = shiftdim(data.trainingtemplate(1,2,:))';
d2 = B(i,:);
C(i) = corr2(d1,d2);
end
r(2) = mean(C);
set(handles.text55,'String',int2str(r(2)*1000));
elseif get(handles.popupmenu_select_target,'Value')==4
data.trainingtemplate(1,3,:) = m;
data.templatesize(3) = length(m);
C = zeros(1,N-1);
for i=1:N-1
d1 = shiftdim(data.trainingtemplate(1,3,:))';
d2 = B(i,:);
C(i) = corr2(d1,d2);
end
r(3) = mean(C);
set(handles.text56,'String',int2str(r(3)*1000));
elseif get(handles.popupmenu_select_target,'Value')==5
data.trainingtemplate(1,4,:) = m;
data.templatesize(4) = length(m);
C = zeros(1,N-1);
for i=1:N-1
d1 = shiftdim(data.trainingtemplate(1,4,:))';
d2 = B(i,:);
C(i) = corr2(d1,d2);
end
r(4) = mean(C);
set(handles.text57,'String',int2str(r(4)*1000));
end
% Update GUI
data.trainingoffset = 0;
set(data.com,'UserData',data);
set(handles.toggle_blind,'Value',0);
set(handles.toggle_blind,'String','Blind');
fprintf(data.com,'%s','B\n'); % Unblind after training
set(handles.text34,'String',int2str(length(m)));
data.templatequality = r*1000;
set(data.com,'UserData',data);
set(handles.pushbutton_go,'Enable','On');
set(handles.pushbutton_go,'String','Go');

Answers (0)

Categories

Find more on Signal Integrity Kits for Industry Standards in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!