A Cardiovascular Simulator for Research 1.0.0

File: <base>/src/plot_cfvr.c (4,531 bytes)
/* The function plot_cfvr.c displays cardiac function and venous return
   curves by writing Gnuplot commands to the gpcommands file in the /tmp
   directory and then invoking gnuplot on the file.

   Function arguments:
         outputfilein - MATLAB array of the prefix name of the file containing             
                        the cardiac function/venous return numerics 
         covrformatin - MATLAB array indicating the desired plotting format
                       
   Function outputs: none */

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <matlab.h>

void mlfPlot_cfvr(const mxArray *outputfilein, const mxArray *covrformatin)
{

    /* Declaring variables. */
    char command[128], *outputfile;
    FILE *fp, *fpnew;
    double covrformat;

    /* Converting MATLAB types to C types. */
    outputfile=mxArrayToString(outputfilein);
    covrformat=mxGetScalar(covrformatin);

    /* If only the curve of the current simulation is desired for display
       or there is currently no /tmp/gpcommands file. */
    fp = fopen("/tmp/gpcommands","r");
    if (covrformat == 0 | covrformat == 2 | fp == 0)
    {
	
	fclose(fp);
	/* Delete existing gpcommands file. */
	system("rm -f /tmp/gpcommands");

	/* Creating new gpcommands file. */
	fpnew = fopen("/tmp/gpcommands","w");
	sprintf(command,"set tics out");
	fprintf(fpnew,"%s\n",command);
	sprintf(command,"set title ''");
	fprintf(fpnew,"%s\n",command);

	/* For cardiac function or venous return curve display. */
	if (covrformat == 0)
	{
	    sprintf(command,"set xlabel 'mPra [mmHg]'");
	}
	/* For cardiac output versus systemic arterial pressure curve display. */
	else
	{
	    sprintf(command,"set xlabel 'mPa [mmHg]'");
	}    
        fprintf(fpnew,"%s\n",command);

	/* For cardiac function or venous return curve display. */
	if (covrformat == 0)
	{
	    sprintf(command,"set ylabel 'mql,mqv [l/min]'");
	}
	/* For cardiac output versus systemic arterial pressure curve display. */
	else
	{
	    sprintf(command,"set ylabel 'mql [l/min]'");
	}    
	fprintf(fpnew,"%s\n",command);
	sprintf(command,"set xrange [:]");
	fprintf(fpnew,"%s\n",command);
	sprintf(command,"set yrange [:]");
	fprintf(fpnew,"%s\n",command);

	/* For cardiac function or venous return curve display. */
	if (covrformat == 0) 
	{
	    fprintf(fpnew,"plot '%s.txt' using 1:2 notitle with lines\n",outputfile);
	}
	/* For cardiac output versus systemic arterial pressure curve display. */
	else
	{
	    fprintf(fpnew,"plot '%s.txt' using 3:2 notitle with lines\n",outputfile);
	}
	fclose(fpnew);

	/* Copying gpcommands to a temporary file. */
	system("cp /tmp/gpcommands /tmp/gpcommands.orig");

	/* Adding a pause command to keep display window open until user
	   desires to close it to the gpcommands file. */
	fpnew = fopen("/tmp/gpcommands","a");
	sprintf(command,"pause -1 \"Press RETURN to dismiss the plot window: \"");
	fprintf(fpnew,"%s\n",command);
	fclose(fpnew);

	/* Invoking gnuplot. */
	system("gnuplot /tmp/gpcommands < /dev/tty");


	/* Copying temporary file -- without pause command -- to gpcommands. */
	system("mv -f /tmp/gpcommands.orig /tmp/gpcommands");

    }

    /* If multiple curves per a window are desired and a /tmp/gpcommands file 
       currently exists. */
    else
    {

	fclose(fp);

	/* Adding new Gnuplot commands to existing gpcommands file. */

	/* For cardiac function or venous return curve display. */
	if (covrformat == 1)
	{
	    sprintf(command,"replot \'%s.txt\' using 1:2 notitle with lines",outputfile);
	}
	/* For cardiac output versus systemic arterial pressure curve display. */
	else
	{
	    sprintf(command,"replot \'%s.txt\' using 3:2 notitle with lines",outputfile);
	}
	fpnew = fopen("/tmp/gpcommands","a");
	fprintf(fpnew,"%s\n",command);
	fclose(fpnew);

	/* Copying gpcommands to a temporary file. */
	system("cp /tmp/gpcommands /tmp/gpcommands.orig");

	/* Adding a pause command to keep display window open until user
	   desires to close it to the gpcommands file. */
	fpnew = fopen("/tmp/gpcommands","a");
	sprintf(command,"pause -1 \"Press RETURN to dismiss the plot window: ");
	fprintf(fpnew,"%s\n",command);
	fclose(fpnew);

	/* Invoking gnuplot. */
	system("gnuplot /tmp/gpcommands < /dev/tty");

	/* Copying temporary file -- without pause command -- to gpcommands. */
	system("mv -f /tmp/gpcommands.orig /tmp/gpcommands");

    }

    /* Freeing allocated dynamic memory and returning to rcvsim.m */
    mxFree(outputfile);
    return;

}