function do_simulation(varargin)
	simul_out_filename='simul_out.dat';
	measure_sec_login='';
	t='';lo='';p='';m='';cn='';si='';sa='';n='';bn='';
	for i=(1:1:nargin/2) 
		switch varargin{2*i-1}
			case 't'
				t=varargin{2*i};
			case 'lo'
				lo=varargin{2*i};
			case 'p'
				p=varargin{2*i};
			case 'm'
				m=varargin{2*i};
			case 'ert'
				ert=varargin{2*i};
			case 'cn'
                cn=varargin{2*i};
			case 'si'
				si=varargin{2*i};
			case 'sa'
				sa=varargin{2*i};
			case 'n'
				n=varargin{2*i};
			case 'bn'
				disp('Benchmark number (bn) set automtically');
			case 'target'
				target_login=varargin{2*i};
			case 'target_pass'
				target_pass=varargin{2*i};
			case 'measure'
				measure_login=varargin{2*i};
			case 'measure_sec'
				measure_sec_login=varargin{2*i};
			otherwise
					disp(['Unknown option: ' varargin{2*i-1}]);
		end
	end

	disp('new simulation to start');	
	
	%---------SETTING SIMULATION VARIABLES-----------------------------
	if (strcmp(t,'lat'))
		measure_login=target_login;
		measure_file_dir='';
	elseif (strcmp(t,'can'))
		measure_file_dir='/run/';
	end
	[~,bnum]=system('./getNewBN.sh');
	bn=bnum(1:end-1);
	if (strcmp(ert,'MTW'))
		Cell=strsplit(target_login,'@');
		target_user=char(Cell(1));
		target_ip=char(Cell(2));
	end


	%%----------GATHERING INFO ABOUT THE TARGET------------------------
	%check availability
	[~,check_avail]=system(['./checkMachine.sh ' target_login]);
	disp(check_avail);
	if (~strcmp(check_avail(1:end-1),'online'))
		disp('Target not available. Quitting.');
		return;
	end
	%check measurement machine availability
	[~,check_avail]=system(['./checkMachine.sh ' measure_login]);
	if (~strcmp(check_avail(1:end-1),'online'))
		disp('Measurement machine not available. Quitting.');
		return;
	end
	if (~strcmp(measure_sec_login,''))
		%check measurement machine availability
		[~,check_avail]=system(['./checkMachine.sh ' measure_login]);
		if (~strcmp(check_avail(1:end-1),'online'))
			disp('Measurement machine not available. Quitting.');
			return;
		end
	end
	%get to know the hardware
	[~,hw_test]=system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' ''if [ -n "`lscpu | grep "CPU max MHz:" | grep "1200"`" -a -n "`cat /proc/cpuinfo | grep -o "BCM2835"`" ]; then echo "1"; else echo "0"; fi''; }']);
	if (strcmp(hw_test(1:end-1),'1'))
		hw='RPi-v3';
	else
		[~,hw_test]=system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' ''if [ -n "`lscpu | grep "CPU max MHz:" | grep "900"`"  -a -n "`cat /proc/cpuinfo | grep -o "BCM2835"`" ]; then echo "1"; else echo "0"; fi''; }']);
		if (strcmp(hw_test(1:end-1),'1'))
			hw='RPi-v2';
		else
			[~,hw_test]=system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' ''if [ -n "`lscpu | grep -o ''x86''`" ]; then echo "1"; else echo "0"; fi''; }']);
			if (strcmp(hw_test(1:end-1),'1'))
				hw='x86';
			else
				[~,hw_test]=system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' ''if [ -n "`cat /proc/cpuinfo | grep -o ''Zynq''`" ]; then echo "1"; else echo "0"; fi''; }']);
				if (strcmp(hw_test(1:end-1),'1'))
					hw='Zynq';
				else
					hw='Unknown';
				end
			end
		end
	end
	
	%kernel patch
	[~,k]=system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' ''if [ -n "`uname -a | grep "PREEMPT"`" ]; then echo "RT"; else echo "non-RT"; fi''; }']);
	k=k(1:end-1);
	%kernel release - version kernel
	[~,vk]=system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' uname -r | awk -F. ''{print $1"."$2}''; }']);
	%remove new line at the end
	vk=vk(1:end-1);
	%remove new line at the end
	
	%------------------CHECKING FEASIBILITY OF THE CONFIGURATION-------
	%MathWorks ert available only for Raspberry
	if (strcmp(ert,'MTW') && ~((strcmp(hw,'RPi-v2')) || (strcmp(hw,'RPi-v3'))))
		disp('Matlab ert only for RPI. Quitting.')
		return
	end
	%CAN availability
	if (strcmp(t,'can'))
		%target
		[~,can]=system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' ''/sbin/ip link | grep -o "can[0-9]" | head -1''; }']);
		cann=[can 'xxxx'];
		disp(['cant=' cann(1:4)]);
		if (~strcmp(cann(1:3),'can'))
			disp('CAN not available on the target. Quitting.');
			return
		else
			cant=cann(1:4);
		end
		%measurement machine
		[~,can]=system(['{ unset LD_LIBRARY_PATH; ssh ' measure_login ' ''/sbin/ip link | grep -o "can[0-9]" | head -1''; }']);
		cann=[can 'xxxx'];
		disp(['canm=' cann(1:4)]);
		if (~strcmp(cann(1:3),'can'))
			disp('CAN not available on the measurement machine. Quitting.');
			return
		else
			canm=cann(1:4);
		end;
		if (~strcmp(measure_sec_login,''))
			%secondary measurement machine
			[~,can]=system(['{ unset LD_LIBRARY_PATH; ssh ' measure_sec_login ' ''/sbin/ip link | grep -o "can[0-9]" | head -1''; }']);
			cann=[can 'xxxx'];
			disp(['cans=' cann(1:4)]);
			if (~strcmp(cann(1:3),'can'))
				disp('CAN not available on the secondary measurement machine. Quitting.');
				return
			else
				cans=cann(1:4);
			end;
		end
	end
	
	%choose apropriate model
	if (strcmp(t,'lat'))
		if (strcmp(ert,'MTW'))
			model_name='latency_demo';
		elseif (strcmp(ert,'CTU'))
			model_name='latency_demo_ERT_L';
		end
	elseif (strcmp(t,'can'))
		if (strcmp(ert,'MTW'))
			model_name='can_test';
		elseif (strcmp(ert,'CTU'))
			model_name='can_test_ERT_L';
		end
	end

	%------FIRST STAGE OF MODIFYING MODEL PROPERTIES-------------------
	if ((strcmp(t,'lat') || (strcmp(t,'can')))  && strcmp(ert,'MTW'))
		%edit model XML file - set priority
		%this cant be done by 'set_param'
		if (strcmp(t,'lat'))
			xml_file='configSet1.xml';
		elseif (strcmp(t,'can'))
			xml_file='configSet0.xml';
		end
		model_dir=['../matlab/toolbox/latency_tests/model_code/' model_name '/simulink/'];
    
        sed_command=['"s/<Field Name=\"RTOSBaseRateTaskPriority\" Class=\"char\">[0-9][0-9]<\/Field>/<Field Name=\"RTOSBaseRateTaskPriority\" Class=\"char\">' p '<\/Field>/"'];
        %make backup copy of the xml
        system(['cp ' model_dir xml_file ' ' model_dir '../../' xml_file '.bak']);
        %modify the xml
        system(['cat ' model_dir '../../' xml_file '.bak | sed -e ' sed_command ' > ' model_dir xml_file ]);
        %remove old model
        system(['rm -f ' model_dir '../../../' model_name '.slx']);
		%rebuilt the model
        !{ cd ../matlab/toolbox/latency_tests/; make models; }
	end
            
	%---------APPLYING PATCHES AND LOADS, TARGET PREPARING-------------
    %backup files that are going to be patched
	if (strcmp(cn,'Yes') || strcmp(m,'CF'))
		!{ cd /usr/local/MATLAB/R2016b; sudo cp toolbox/target/codertarget/rtos/src/linuxinitialize.c toolbox/target/codertarget/rtos/src/linuxinitialize.c.bak; }
		!{ cd /usr/local/MATLAB/R2016b; sudo cp toolbox/target/codertarget/rtos/src/linuxinitialize.cpp toolbox/target/codertarget/rtos/src/linuxinitialize.cpp.bak; }
	end
	%patch matlab tlc with nanosleep
	if (strcmp(cn,'Yes')) 
		disp('aplying patch CLOCK_NANOSLEEP');
        ! sudo cp ../matlab/patch-RT-production/2-nanosleep.patch /usr/local/MATLAB/R2016b/ ;
        !{ cd /usr/local/MATLAB/R2016b; sudo patch -p1 -N  <2-nanosleep.patch; }
	end

	%patch matlab tlc with memory lock
	if (strcmp(m,'CF')) 
		disp('aplying memory lock patch');
		! sudo cp ../matlab/patch-RT-production/1-mlockall.patch /usr/local/MATLAB/R2016b/ ;
		!{ cd /usr/local/MATLAB/R2016b; sudo patch -p1 -N  <1-mlockall.patch; }
	end
    
	%start to load the target
	if (strcmp(lo,'8W1F'))
		disp('Starting load 8xwhile loop, 1xFind-in-loop');
		system(['{ unset LD_LIBRARY_PATH; scp startLoad.sh termLoad.sh ' target_login ': >/dev/null; }']);
		system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' "setsid ./startLoad.sh >/dev/null 2>&1 </dev/null &"; }']);
	elseif (~strcmp(lo,'No'))
		disp('Load not recognized. Setting load 8W1F.')
		lo='8W1F';
		system(['{ unset LD_LIBRARY_PATH; scp startLoad.sh termLoad.sh ' target_login ': >/dev/null; }']);
		system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' "setsid ./startLoad.sh >/dev/null 2>&1 </dev/null &"; }']);
	end

	%remove possible old simulation data
	if (strcmp(t,'lat') || strcmp(t,'can'))
		system(['{ unset LD_LIBRARY_PATH; ssh ' measure_login ' "rm -f ' measure_file_dir simul_out_filename '"; }']);
	end
	
	%can preparing
	if (strcmp(t,'can'))
		disp('Preparing CAN.');
		system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' "/sbin/ip link set ' cant ' up type can bitrate 500000"; }']);
		system(['{ unset LD_LIBRARY_PATH; ssh ' measure_login ' "/sbin/ip link set ' canm ' up type can bitrate 500000"; }']);
		%move logging scripts
		disp('Moving logging scripts.');
		system(['{ unset LD_LIBRARY_PATH; scp startCANLog.sh termCANLog.sh ' measure_login ': >/dev/null; }']);
		%start can logging
		disp('CAN logging started on measurement machine.');
		system(['{ unset LD_LIBRARY_PATH; ssh ' measure_login ' "setsid ./startCANLog.sh >/dev/null 2>&1 </dev/null &"; }']);	
		if (~strcmp(measure_sec_login,''))
			system(['{ unset LD_LIBRARY_PATH; ssh ' measure_sec_login ' "/sbin/ip link set ' cans ' up type can bitrate 500000"; }']);
			system(['{ unset LD_LIBRARY_PATH; scp startCANLog.sh termCANLog.sh ' measure_sec_login ': >/dev/null; }']);
			system(['{ unset LD_LIBRARY_PATH; ssh ' measure_sec_login ' "setsid ./startCANLog.sh >/dev/null 2>&1 </dev/null &"; }']);	
		end
	end
		
	%------SECOND STAGE OF MODIFYING MOD. PROP.---BUILD & RUN----------
	%switch to the build directory, open the system and start
	%simulation
	!rm -rf ../__build
	!mkdir -p ../__build
	cd ../__build

	if (strcmp(t,'lat') || strcmp(t,'can'))
		open_system(['../matlab/toolbox/latency_tests/' model_name]);
		set_param(model_name,'GenerateReport','Off','LaunchReport','Off');
		set_param(model_name,'StopTime',si,'FixedStep',sa);    
		pause(10);

		if (strcmp(ert,'MTW'))
			%start the build process
			disp('Starting the model with Mathworks ert');
			raspberrypi(target_ip,target_user,target_pass);
			set_param(model_name, 'SimulationCommand', 'start');
			%wait for the system 
			disp(['waiting for the model to end ' si ' sec']);
			pause(str2double(si));
			wt=60; %aditional wait
			disp(['waiting additional ' num2str(wt) ' sec']);
			pause(wt);

			disp('Simulation ended.');
		elseif (strcmp(ert,'CTU'))
			if (strcmp(hw,'RPi-v2') || strcmp(hw,'RPi-v3') || strcmp(hw,'Zynq'))
				make_cmd=get_param(model_name,'MakeCommand');
				make_cmd=[make_cmd ' CC=arm-rpi-linux-gnueabihf-gcc'];
				set_param(model_name,'MakeCommand',make_cmd);
			end
			%start the build process
			disp('Starting the build model with CTU ert');
			system(['rm -f ' model_name]);
			slbuild(model_name);
			disp('Build completed. Moving the model to the target.');
			system(['{ unset LD_LIBRARY_PATH; scp ' model_name ' ' target_login ': >/dev/null; }']);
			disp('Starting the model and waiting for it to finish.');
			system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' "schedtool -F -p ' p ' -e ./' model_name '"; }']);                
		end
	end

	%------TERMINATE THE LOAD, CLOSE AND RESTORE-----------------------
	%terminate the load
	if (strcmp(lo,'8W1F'))
		system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' "./termLoad.sh"; }']);
        system(['{ unset LD_LIBRARY_PATH; ssh ' target_login ' rm -f startLoad.sh termLoad.sh; }']);
	end
	
	%stop CAN logging
	if (strcmp(t,'can'))
		system(['{ unset LD_LIBRARY_PATH; ssh ' measure_login ' "./termCANLog.sh"; }']);
		system(['{ unset LD_LIBRARY_PATH; ssh ' measure_login ' rm -f startCANLog.sh termCANLog.sh; }']);
		if (~strcmp(measure_sec_login,''))
			system(['{ unset LD_LIBRARY_PATH; ssh ' measure_sec_login ' "./termCANLog.sh"; }']);
			system(['{ unset LD_LIBRARY_PATH; ssh ' measure_sec_login ' rm -f startCANLog.sh termCANLog.sh; }']);	
		end
	end

	%restore patched file
	if (strcmp(cn,'Yes') || strcmp(m,'CF'))
		!{ cd /usr/local/MATLAB/R2016b; sudo mv toolbox/target/codertarget/rtos/src/linuxinitialize.c.bak toolbox/target/codertarget/rtos/src/linuxinitialize.c; }
		!{ cd /usr/local/MATLAB/R2016b; sudo mv toolbox/target/codertarget/rtos/src/linuxinitialize.cpp.bak toolbox/target/codertarget/rtos/src/linuxinitialize.cpp; }
		!{ cd /usr/local/MATLAB/R2016b; sudo rm -f 1-mlockall.patch; }
		!{ cd /usr/local/MATLAB/R2016b; sudo rm -f 2-nanosleep.patch; }
	end;

	%close system
	if (strcmp(t,'lat') || strcmp(t,'can'))
		%close system without saving
		close_system(model_name,0);
	end
		
	%restore models settings
	if ((strcmp(t,'lat') || (strcmp(t,'can')))  && strcmp(ert,'MTW'))
		%restore original model xml
		system(['mv ' model_dir '../../' xml_file '.bak ' model_dir xml_file]);
	end

	%move back to tools directory
	cd ../tools
		
	%-------------COMPOSE DATA FILENAME--------------------------------    
	newname=['t' t '_hw' hw '_lo' lo '_k' k '_vk' vk '_p' p...
			'_ert' ert '_m' m '_cn' cn '_si' si '_sa' sa '_n' n '_bn'...
			bn '_.rdat'];
	fprintf(2,['new file: ' newname '\n']);
        
	if (strcmp(t,'lat') || strcmp(t,'can'))
		%rename the data file
		system(['{ unset LD_LIBRARY_PATH; ssh ' measure_login ' "mv '... 
		measure_file_dir simul_out_filename ' ' measure_file_dir newname '"; }']);
		%move it home
		system(['{ unset LD_LIBRARY_PATH; mkdir -p ../data/__new_measurements; scp '...
			measure_login ':' measure_file_dir newname ' ../data/__new_measurements; }']);
		%delete the file on the target
		system(['{ unset LD_LIBRARY_PATH; ssh ' measure_login ' "rm -f '... 
			measure_file_dir newname '"; }']);
		if (~strcmp(measure_sec_login,''))
			newname=['t' t '_hw' hw '_lo' lo '_k' k '_vk' vk '_p' p...
			'_ert' ert '_m' m '_cn' cn '_si' si '_sa' sa '_nmsec' n '_bn'...
			bn '_.rdat'];
			system(['{ unset LD_LIBRARY_PATH; ssh ' measure_sec_login ' "mv '...
				measure_file_dir simul_out_filename ' ' measure_file_dir newname '"; }']);
			system(['{ unset LD_LIBRARY_PATH; mkdir -p ../data/__new_measurements; scp '...
				measure_sec_login ':' measure_file_dir newname ' ../data/__new_measurements; }']);
			system(['{ unset LD_LIBRARY_PATH; ssh ' measure_sec_login ' "rm -f '... 
				measure_file_dir newname '"; }']);
		end

	end
end