Using MATLAB to plot data generated by other programs:
For the C language:
/* This file may be compiled using the format:
gcc filename.c -o exefile -lm
Running this program creates a file named "outvar.dat". The
data for a cosine and sine function is calaculated and saved
as three columns of numbers.
To plot the data within MATLAB, you may use the following from a
MATLAB window:
>> load outvar.dat
>> x = outvar(:,1);
>> y = outvar(:,2);
>> z = outvar(:,3);
>> plot(x,y)
>> hold on
>> plot(x,z)
>> hold off
*/
#include <stdio.h> /* standard Input/Output */
#include <stdlib.h> /* standard library */
#include <math.h> /* math library */
main()
{
double x,y,z; /* variables used */
FILE *fp; /* file pointer */
/* Check to see if there are any file errors. */
/* This will either create a new file OUTVAR.DAT, or */
/* write over an existing version of the file. */
if ( (fp = fopen("outvar.dat", "w")) == NULL)
{
printf("Cant open OUTVAR.DAT \n");
exit(1);
}
/* This loop is used to save y=cos(x).*/
for(x=0; x<=10; x += 0.1)
{
y = cos(x);
z = sin(x);
fprintf(fp, "%f %f %f",x,y,z); /* save the x and y values */
fprintf(fp, "\n"); /* create a line break */
}
fclose(fp); /* close the file */
}
For Other High Level Languages: If you want to use Fortran, Pascal, Basic, or some other
language and MATLAB to generate plots you proceed in a similar manner to how you do
for C. Note that any space delimited ASCII data file can be read by MATLAB (i.e.
columns of data seperated by any number of "blank" spaces). Hence, if you use any high
level language simply output your data into an ASCII file and use the MATLAB
commands given above.