function [trainImgs, testImgs, trainLabels, testLabels]=mulSourceSplitData(imgPath,srcDomains,tgtDomain,sy)
numClass = 10;

num_train_tgt = 3;

trainImgs = cell(0);
testImgs = cell(0);
trainLabels = [];
testLabels=[];

% load images from src domain
for iDomain = 1:length(srcDomains)
    srcDomain = srcDomains{iDomain};
    num_train_src = imageSetSize(srcDomain);
   
    objectFolders = dir(fullfile(imgPath,srcDomain,'images'));
    iClass = 0;
    for iFolder = 1:length(objectFolders)
        if (~objectFolders(iFolder).isdir)
            continue
        end
        sub_path = objectFolders(iFolder).name;
        if(strcmp(sub_path,'.') || strcmp(sub_path,'..')||strcmp(sub_path,'.svn'))
            continue;
        end
        iClass= iClass+1;
        sub_path = [srcDomain '/images/' objectFolders(iFolder).name];
       
        imgList = dir(fullfile(imgPath,sub_path,'*.jpg'));
        idx= randperm(length(imgList));
        for iImg= 1:min(length(imgList),num_train_src)
            img = imread(fullfile(imgPath,sub_path,imgList(idx(iImg)).name));
            img = imresize(img,[NaN sy]);
            trainImgs{end+1}=img;
            trainLabels = [trainLabels; iClass];
        end
    end
end % iDomain
% load images from target domain

objectFolders = dir(fullfile(imgPath,tgtDomain,'images'));
iClass = 0;
num_train_tgt = 3;
for iFolder = 1:length(objectFolders)
    if (~objectFolders(iFolder).isdir)
        continue
    end
    sub_path = objectFolders(iFolder).name;
    if(strcmp(sub_path,'.') || strcmp(sub_path,'..')||strcmp(sub_path,'.svn'))
        continue;
    end
    iClass = iClass+1;
    sub_path = [tgtDomain '/images/' objectFolders(iFolder).name];
    imgList = dir(fullfile(imgPath,sub_path,'*.jpg'));
    idx= randperm(length(imgList));
    for iImg= 1:num_train_tgt
        img = imread(fullfile(imgPath,sub_path,imgList(idx(iImg)).name));
        img = imresize(img,[NaN sy]);
        trainImgs{end+1}=img;
        trainLabels = [trainLabels; iClass];
    end
   
    for iImg = num_train_tgt+1:length(idx)
        img = imread(fullfile(imgPath,sub_path,imgList(idx(iImg)).name));
        img = imresize(img,[NaN sy]);
        testImgs{end+1}=img;
        testLabels = [testLabels; iClass];
    end
end % end for classes in target domain.

end % end of function

function num_train_src = imageSetSize(srcDomain)
if strcmp(srcDomain,'dslr') || strcmp(srcDomain,'webcam')
    num_train_src = 8;
else
    num_train_src = 20;
end
end