add example with LabWindow
[scilab.git] / scilab / modules / core / examples / call_scilab / c / LabWindow / CallsciC.c
1 /* Allan CORNET */
2 /* Scilab, INRIA 2004 */
3 /* Only For Windows */
4 /*--------------------------------------------------------------------------*/
5 #pragma comment(lib, "../../../../../../bin/libScilab.lib")
6 #pragma comment(lib, "../../../../../../bin/MALLOC.lib")
7 /*--------------------------------------------------------------------------*/
8 #include <stdlib.h> 
9 #include <stdio.h> 
10 #include <string.h>
11 #include "CallScilab.h"
12 #include "stack-c.h"
13 /*--------------------------------------------------------------------------*/
14 /* See SCI/modules/core/includes/CallScilab.h */
15 /*--------------------------------------------------------------------------*/
16 static int example1(void)
17 {
18         static double A[]={1,2,3,4};  int mA=2,nA=2;
19         static double b[]={4,5};  int mb=2,nb=1;
20
21
22         /* Create Scilab matrices A and b */
23         WriteMatrix("A", &mA, &nA, A);
24         WriteMatrix("b", &mb, &nb, b);
25
26         SendScilabJob("disp('A=');");
27         SendScilabJob("disp(A);");
28         SendScilabJob("disp('b=');");
29         SendScilabJob("disp(b);");
30         SendScilabJob("disp('x=A\\b');");
31
32         if ( SendScilabJob("A,b,x=A\\b;") != 0) 
33         {
34                 fprintf(stdout,"Error occured during scilab execution (SendScilabJob)\n");
35         }
36         else 
37         {
38                 double *cxtmp=NULL;
39                 int m,n,lp,i;
40
41                 /* Get m and n */
42                 GetMatrixptr("x", &m, &n, &lp);
43
44                 cxtmp=(double*)malloc((m*n)*sizeof(double));
45
46                 ReadMatrix("x", &m, &n, cxtmp);
47
48                 for(i=0;i<m*n;i++)
49                 {
50                         fprintf(stdout,"x[%d] = %5.2f\n",i,cxtmp[i]);
51                 }
52
53                 if (cxtmp) 
54                 {
55                         free(cxtmp);
56                         cxtmp=NULL;
57                 }
58         }
59         return 0;
60 }
61 /*--------------------------------------------------------------------------*/
62 static int example2(void)
63 {
64         int code=0;
65
66         char **JOBS = NULL;
67         const int SizeJOBS = 5;
68         int i=0;
69         int m = 0, n = 0;
70         double *cxtmp = NULL;
71
72         JOBS=(char**)malloc(sizeof(char**)*SizeJOBS);
73
74         for (i=0;i<SizeJOBS;i++)
75         {
76                 JOBS[i]=(char*)malloc(sizeof(char*)*1024);
77         }
78
79         strcpy(JOBS[0],"A=1 ..");
80         strcpy(JOBS[1],"+3;");
81         strcpy(JOBS[2],"B = 8;");
82         strcpy(JOBS[3],"+3;");
83     strcpy(JOBS[4],"C=A+B;"); /* C = 12 */
84
85         
86         for(i=0;i<SizeJOBS;i++)
87         {
88                 printf("JOBS[%d] = %s\n",i,JOBS[i]);
89         }
90         
91         code=SendScilabJobs(JOBS,SizeJOBS);
92
93         if (code)
94         {
95                 char lastjob[4096]; // bsiz in scilab 4096 max
96                 if (GetLastJob(lastjob,4096))
97                 {
98                         printf("Error %s\n",lastjob);
99                 }
100         }
101         
102         m = 1; n = 1;
103         cxtmp = (double*)malloc((m*n)*sizeof(double));
104
105         ReadMatrix("C", &m, &n, cxtmp);
106
107         printf("Result :\n");
108         for(i=0;i<m*n;i++)
109         {
110                 fprintf(stdout,"C[%d] = %5.2f\n",i,cxtmp[i]);
111         }
112
113         for (i=0;i<SizeJOBS;i++)
114         {
115                 if (JOBS[i]) {free(JOBS[i]);JOBS[i]=NULL;}
116         }
117         return 1;
118 }
119 /*--------------------------------------------------------------------------*/
120 static int example3(void)
121 {
122         SendScilabJob("plot3d();quit;");
123         printf("\nClose Graphical Windows to close this example.\n");
124         while( ScilabHaveAGraph() )
125         {
126                 ScilabDoOneEvent();
127         }
128         return 1; 
129 }
130 /*--------------------------------------------------------------------------*/
131 int main(void)
132 /* int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR szCmdLine, int iCmdShow) */
133 {
134         if ( StartScilab(NULL,NULL,NULL) == FALSE ) printf("Error : StartScilab\n");
135         printf("\nexample 1\n");
136         example1();
137         printf("\npress return\n");
138         getchar();
139         
140         printf("\nexample 2\n");
141         example2();
142         printf("\npress return\n");
143         getchar();
144         
145         printf("\nexample 3\n");
146         example3();
147         printf("\npress return\n");
148         getchar();
149
150         
151         if ( TerminateScilab(NULL) == FALSE ) printf("Error : TerminateScilab\n");
152         return 0;
153 }
154 /*--------------------------------------------------------------------------*/