I found the answer, one should use the timer object UserData property to handle input output in timer: here is the change in the code:
% define constants
total_time = 12;
swap_count = 3;
swap_period = floor(total_time/swap_count);
% assign output variable from callback function
X = 'AB';
% crete a timer object and set properties
t_obj = timer;
set(t_obj, 'StartDelay', swap_period);
set(t_obj, 'Period', swap_period);
set(t_obj, 'TasksToExecute', swap_count);
set(t_obj, 'ExecutionMode', 'fixedRate');
set(t_obj, 'UserData', X);
set(t_obj, 'StartFcn',{@swap_fcn});
set(t_obj, 'TimerFcn',{@swap_fcn});
set(t_obj, 'StopFcn',{@swap_fcn});
% start timer loop
start(t_obj);
pause(total_time+1);
stop(t_obj);
delete(t_obj);
% callback function
function [X] = swap_fcn(obj, event)
% event properties
event_type = event.Type;
event_time = datestr(event.Data.time,'HH:MM:SS, dd mmm YYYY');
% get times executed
exec_count = get(obj, 'TasksExecuted');
% get current user data
X = get(obj, 'UserData');
% write step and write to file
switch event_type
case 'StartFcn'
fprintf('start experiment: %s\n',event_time);
disp(X);
case 'TimerFcn'
fprintf('swap%02d: %s\n',exec_count, event_time);
X = fliplr(X);
disp(X);
case 'StopFcn'
fprintf('stop experiment: %s\n',event_time);
disp(X);
end
% update any changes
set(obj,'UserData',X);
end