Home | Develop | Download | Contact
extras.h
1 /*
2  * extras.h
3  *
4  * Copyright 2011 Fernando Pujaico Rivera <fernando.pujaico.rivera@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef __EXTRAS_H__
24 #define __EXTRAS_H__
25 
26 bool pds_octave_plot_points( const Pds::Matrix &X,
27  const Pds::Vector &Y,
28  const char *labelx,
29  const char *labely,
30  const char *octfile,
31  const char *imgfile)
32 {
33  unsigned int i;
34  FILE *fd=NULL;
35  char *orden=NULL;
36 
37  unsigned int PDS_OCTAVE_MARKERSIZE=10;
38  unsigned int PDS_OCTAVE_FONTSIZE=16;
39  unsigned int PDS_OCTAVE_LINEWIDTH=4;
40 
41  if(X.IsEmpty()) return false;
42  if(Y.IsEmpty()) return false;
43  if(labelx==NULL) return false;
44  if(labely==NULL) return false;
45  if(imgfile==NULL) return false;
46 
47  char *OCTAVEFILE=NULL;
48  if(octfile==NULL)
49  {
50  OCTAVEFILE=(char*)calloc(64,sizeof(char));
51  if(OCTAVEFILE==NULL) return false;
52  sprintf(OCTAVEFILE,"temp%d.m",rand());
53  }
54  else
55  {
56  OCTAVEFILE=strdup(octfile);
57  if(OCTAVEFILE==NULL) return false;
58  }
59 
60 
61 
62  fd=fopen(OCTAVEFILE,"w");
63  fprintf(fd,"%c\n",'%');
64  if(fd==NULL) return false;
65  fprintf(fd,"Y=[\n");
66  for(i=0 ; i<Y.Nlin() ; i++)
67  {
68  if(i!=Y.LinEnd()) fprintf(fd,"%e\n",Y.Get(i));
69  else fprintf(fd,"%e;\n",Y.Get(i));
70  }
71  fprintf(fd,"];\n");
72  fprintf(fd,"X=[\n");
73  for(i=0 ; i<X.Nlin() ; i++)
74  {
75  if(i!=X.LinEnd()) fprintf(fd,"%e \n",X.Get(i));
76  else fprintf(fd,"%e;\n",X.Get(i));
77  }
78  fprintf(fd,"];\n");
79 
80  fprintf(fd,"h=figure(1);\n");// h=figure('Visible', 'off');
81  fprintf(fd,"hp=plot(X,Y,\'ob\');\n");
82  //fprintf(fd,"daspect([1 1 1]);\n");
83  fprintf(fd,"set(hp,\'markersize\',%d);\n",PDS_OCTAVE_MARKERSIZE);
84  fprintf(fd,"set(hp,\'linewidth\',%d);\n",PDS_OCTAVE_LINEWIDTH);
85  fprintf(fd,"grid on;\n");
86  fprintf(fd,"hx=xlabel(\'%s\');\n",labelx);
87  fprintf(fd,"hy=ylabel(\'%s\');\n",labely);
88  //fprintf(fd,"xlim([min(X),max(X)]);\n");
89  fprintf(fd,"ha = gca(); \n");
90  fprintf(fd,"FONTSIZE=%d;\n",PDS_OCTAVE_FONTSIZE);
91  fprintf(fd,"set(hx,'fontsize',FONTSIZE);\n");
92  fprintf(fd,"set(hy,'fontsize',FONTSIZE);\n");
93  fprintf(fd,"set(ha,'fontsize',FONTSIZE);\n");
94 
95  fprintf(fd,"refresh();\n");
96  fprintf(fd,"saveas (h,\'%s\');\n",imgfile);
97  fprintf(fd,"\n");
98  fprintf(fd,"\n");
99  fclose(fd);
100 
101  orden=(char *)calloc(1,48+strlen(OCTAVEFILE));
102  if(orden==NULL) return false;
103  sprintf(orden,"octave --no-gui -q %s",OCTAVEFILE);
104  i=system(orden);
105 
106  if(octfile==NULL)
107  {
108  remove(OCTAVEFILE);
109  }
110  free(OCTAVEFILE);
111  return true;
112 }
113 
114 bool pds_octave_plot_vector(const Pds::Matrix &X,const char *labelx,const char *labely,const char *octfile,const char *imgfile)
115 {
116  unsigned int i;
117  FILE *fd=NULL;
118  char *orden=NULL;
119 
120  unsigned int PDS_OCTAVE_MARKERSIZE=10;
121  unsigned int PDS_OCTAVE_FONTSIZE=16;
122  unsigned int PDS_OCTAVE_LINEWIDTH=4;
123 
124  if(X.IsEmpty()) return false;
125  if(labelx==NULL) return false;
126  if(labely==NULL) return false;
127  if(imgfile==NULL) return false;
128 
129  char *OCTAVEFILE=NULL;
130  if(octfile==NULL)
131  {
132  OCTAVEFILE=(char*)calloc(64,sizeof(char));
133  if(OCTAVEFILE==NULL) return false;
134  sprintf(OCTAVEFILE,"temp%d.m",rand());
135  }
136  else
137  {
138  OCTAVEFILE=strdup(octfile);
139  if(OCTAVEFILE==NULL) return false;
140  }
141 
142 
143 
144  fd=fopen(OCTAVEFILE,"w");
145  fprintf(fd,"%c\n",'%');
146  if(fd==NULL) return false;
147  fprintf(fd,"X=[\n");
148  for(i=0 ; i<X.Nlin() ; i++)
149  {
150  if(i!=X.LinEnd()) fprintf(fd," %e, %e \n",X.Get(i,0),X.Get(i,1));
151  else fprintf(fd,"%e, %e;\n",X.Get(i,0),X.Get(i,1));
152  }
153  fprintf(fd,"];\n");
154 
155  fprintf(fd,"h=figure(1);\n");// h=figure('Visible', 'off');
156  fprintf(fd,"hp=plot(X(:,1),X(:,2),\'ok\');\n");
157  fprintf(fd,"daspect([1 1 1]);\n");
158  fprintf(fd,"set(hp,\'markersize\',%d);\n",PDS_OCTAVE_MARKERSIZE);
159  fprintf(fd,"set(hp,\'linewidth\',%d);\n",PDS_OCTAVE_LINEWIDTH);
160  fprintf(fd,"grid on;\n");
161  fprintf(fd,"hx=xlabel(\'%s\');\n",labelx);
162  fprintf(fd,"hy=ylabel(\'%s\');\n",labely);
163  //fprintf(fd,"xlim([min(X),max(X)]);\n");
164  fprintf(fd,"ha = gca(); \n");
165  fprintf(fd,"FONTSIZE=%d;\n",PDS_OCTAVE_FONTSIZE);
166  fprintf(fd,"set(hx,'fontsize',FONTSIZE);\n");
167  fprintf(fd,"set(hy,'fontsize',FONTSIZE);\n");
168  fprintf(fd,"set(ha,'fontsize',FONTSIZE);\n");
169 
170  fprintf(fd,"refresh();\n");
171  fprintf(fd,"saveas (h,\'%s\');\n",imgfile);
172  fprintf(fd,"\n");
173  fprintf(fd,"\n");
174  fclose(fd);
175 
176  orden=(char *)calloc(1,48+strlen(OCTAVEFILE));
177  if(orden==NULL) return false;
178  sprintf(orden,"octave --no-gui -q %s",OCTAVEFILE);
179  i=system(orden);
180 
181  if(octfile==NULL)
182  {
183  remove(OCTAVEFILE);
184  }
185  free(OCTAVEFILE);
186  return true;
187 }
188 
189 bool pds_octave_plot_vector(const Pds::Matrix &X,const Pds::Vector &Y,const char *labelx,const char *labely,const char *octfile,const char *imgfile)
190 {
191  unsigned int i;
192  FILE *fd=NULL;
193  char *orden=NULL;
194 
195  unsigned int PDS_OCTAVE_MARKERSIZE=10;
196  unsigned int PDS_OCTAVE_FONTSIZE=16;
197  unsigned int PDS_OCTAVE_LINEWIDTH=4;
198 
199  if(X.IsEmpty()) return false;
200  if(Y.IsEmpty()) return false;
201  if(labelx==NULL) return false;
202  if(labely==NULL) return false;
203  if(imgfile==NULL) return false;
204 
205  char *OCTAVEFILE=NULL;
206  if(octfile==NULL)
207  {
208  OCTAVEFILE=(char*)calloc(64,sizeof(char));
209  if(OCTAVEFILE==NULL) return false;
210  sprintf(OCTAVEFILE,"temp%d.m",rand());
211  }
212  else
213  {
214  OCTAVEFILE=strdup(octfile);
215  if(OCTAVEFILE==NULL) return false;
216  }
217 
218 
219 
220  fd=fopen(OCTAVEFILE,"w");
221  fprintf(fd,"%c\n",'%');
222  if(fd==NULL) return false;
223  fprintf(fd,"Y=[\n");
224  for(i=0 ; i<Y.Nel() ; i++)
225  {
226  if(i!=Y.LinEnd()) fprintf(fd,"%e\n",Y.Get(i));
227  else fprintf(fd,"%e;\n",Y.Get(i));
228  }
229  fprintf(fd,"];\n");
230  fprintf(fd,"X=[\n");
231  for(i=0 ; i<X.Nlin() ; i++)
232  {
233  if(i!=X.LinEnd()) fprintf(fd," %e, %e \n",X.Get(i,0),X.Get(i,1));
234  else fprintf(fd,"%e, %e;\n",X.Get(i,0),X.Get(i,1));
235  }
236  fprintf(fd,"];\n");
237 
238  fprintf(fd,"ID0=find(Y<0.5);\n");
239  fprintf(fd,"ID1=find(Y>=0.5);\n");
240  fprintf(fd,"X0=X(ID0,:);\n");
241  fprintf(fd,"X1=X(ID1,:);\n");
242  fprintf(fd,"h=figure(1);\n");// h=figure('Visible', 'off');
243  fprintf(fd,"hp=plot(X0(:,1),X0(:,2),\'or\',X1(:,1),X1(:,2),\'^b\');\n");
244  fprintf(fd,"daspect([1 1 1]);\n");
245  fprintf(fd,"set(hp,\'markersize\',%d);\n",PDS_OCTAVE_MARKERSIZE);
246  fprintf(fd,"set(hp,\'linewidth\',%d);\n",PDS_OCTAVE_LINEWIDTH);
247  fprintf(fd,"grid on;\n");
248  fprintf(fd,"hx=xlabel(\'%s\');\n",labelx);
249  fprintf(fd,"hy=ylabel(\'%s\');\n",labely);
250  //fprintf(fd,"xlim([min(X),max(X)]);\n");
251  fprintf(fd,"ha = gca(); \n");
252  fprintf(fd,"FONTSIZE=%d;\n",PDS_OCTAVE_FONTSIZE);
253  fprintf(fd,"set(hx,'fontsize',FONTSIZE);\n");
254  fprintf(fd,"set(hy,'fontsize',FONTSIZE);\n");
255  fprintf(fd,"set(ha,'fontsize',FONTSIZE);\n");
256 
257  fprintf(fd,"refresh();\n");
258  fprintf(fd,"saveas (h,\'%s\');\n",imgfile);
259  fprintf(fd,"\n");
260  fprintf(fd,"\n");
261  fclose(fd);
262 
263  orden=(char *)calloc(1,48+strlen(OCTAVEFILE));
264  if(orden==NULL) return false;
265  sprintf(orden,"octave --no-gui -q %s",OCTAVEFILE);
266  i=system(orden);
267 
268  if(octfile==NULL)
269  {
270  remove(OCTAVEFILE);
271  }
272  free(OCTAVEFILE);
273  return true;
274 }
275 
276 #endif /* _ARCHIVO_H_H_ */

Enlaces de interés

HomePage Bazaar Download Bug report Ayuda Developer Feed