function jobs=qstat(state)
% Written by Junjun Mao for Levich Institute Matlab server
% state could be one of these:
% 'all': show all jobs
% 'active': show running and pending jobs
% 'running': show running jobs
% 'finished': show finished jobs
% 'queued': show pending jobs
sched=findResource('scheduler', 'type', 'jobmanager', 'lookupURL', 'poincare.engr.ccny.cuny.edu');
alljobs=sched.jobs;
jobs = [];
switch state
    case 'all'
        jobs = alljobs;
    case 'active'
        for job = alljobs'
            %fieldnames(job)
            %username=get(job, 'UserName')
            state=get(job, 'State');
            if ~strcmp(state, 'finished'),
                    jobs = [jobs job];
            end
        end
    case 'running'
        for job = alljobs'
            state=get(job,'State');
            if strcmp(state,'running'),
                jobs = [jobs job];
            end
        end
    case 'finished'
        for job = alljobs'
            state=get(job,'State');
            if strcmp(state,'finished'),
                jobs = [jobs job];
            end
        end
    case 'queued'
        for job = alljobs'
            state=get(job,'State');
            if strcmp(state,'queued'),
                jobs = [jobs job];
            end
        end
    otherwise
        disp('Usage:');
        disp('   qstat("all"): show all jobs');
        disp('   qstat("active"): show running and pending jobs');
        disp('   qstat("running"): show running jobs');
        disp('   qstat("queued"): show pending jobs');
        disp('   qstat("finished"): show finished jobs');
end