Subversion Repositories gelsvn

Rev

Rev 39 | Rev 136 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
39 bj 1
#include "LinAlgIO.h"
2
#include <fstream>
3
#include <iostream>
4
#include <algorithm>
5
#include <vector>
6
 
7
using namespace std;
8
 
9
namespace
10
{
11
 
47 jab 12
		bool Comp(istreambuf_iterator<char>& Itt,const string str)
13
		{
39 bj 14
 
47 jab 15
				int cChar=0;
16
				bool Ret=true;
17
				while(cChar<str.size() && Ret)
18
				{
19
						Ret=(*Itt==str[cChar]);
20
						++Itt;
21
						cChar++;
22
				}
23
 
24
				return Ret;
39 bj 25
		}
26
 
47 jab 27
		bool FindVar(istreambuf_iterator<char>& Itt,
28
								 istreambuf_iterator<char> End,
29
								 const string VarName)
30
		{
31
				bool bFound=false;
39 bj 32
 
47 jab 33
				while(!bFound)
34
				{
35
						Itt=find(Itt,End,VarName[0]);
36
						if(Itt==End)
37
								return false;
38
						bFound=Comp(Itt,VarName);
39
				}
40
 
41
				return true;
39 bj 42
		}
43
 
44
 
45
}
46
 
47
namespace LinAlg
48
{
47 jab 49
		void ToMatlab(const CMatrix& A,
50
									const std::string& VarName,
51
									const std::string& FileName,
52
									const bool append,
53
									const std::string& Comment)
54
		{
55
				std::ofstream Mfile(FileName.c_str(), 
56
														append ? std::ios::app : std::ios::trunc);
57
 
39 bj 58
 
59
 
47 jab 60
				if(!Comment.empty())
61
						Mfile << "% " << Comment.c_str() << "\n";
62
				Mfile << VarName.c_str() << "=[";
39 bj 63
 
64
 
47 jab 65
				const double* pRow;
39 bj 66
 
47 jab 67
				for(int cRow=0;cRow<A.Rows();cRow++)
68
				{
69
						pRow=A[cRow];
70
						for(int cCol=0;cCol<A.Cols();cCol++)
71
						{
72
								Mfile << "\t" << pRow[cCol];
73
						}
74
						Mfile << ";\n";
75
				}
76
 
77
				Mfile << "];\n\n";
39 bj 78
		}
79
 
80
 
47 jab 81
		void FromMatlab(CMatrix& A,
82
										const std::string& VarName,
83
										const std::string& FileName)
84
		{
85
				ifstream file(FileName.c_str());
86
				istreambuf_iterator<char> Itt(file);
87
				istreambuf_iterator<char> End;
39 bj 88
 
47 jab 89
				string VarId(VarName);
90
				VarId+="=[";
39 bj 91
 
47 jab 92
				FindVar(Itt,End,VarId);
39 bj 93
 
47 jab 94
				vector<double> data;
95
				double elem;
96
				int nCols=0;
39 bj 97
 
47 jab 98
				while((*Itt)!=';')	
99
				{
100
						Itt=(file >> elem );
101
						data.push_back(elem);
102
						nCols++;
103
				}
39 bj 104
 
47 jab 105
				Itt++;
106
				Itt++;
107
				int nElems=nCols;
39 bj 108
 
47 jab 109
				while((*Itt)!=']')
110
				{
111
						while((*Itt)!=';')	
112
						{
113
								Itt=(file >> elem );
114
								data.push_back(elem);
115
								nElems++;
116
						}
117
						Itt++;
118
						Itt++;
119
				}
39 bj 120
 
47 jab 121
				A.Resize(nElems/nCols,nCols);
39 bj 122
 
47 jab 123
				memcpy(A[0],&(data[0]),sizeof(double)*data.size());
39 bj 124
 
125
 
126
/*	for(int cI=0;cI<20;cI++)
47 jab 127
		{
39 bj 128
		cout << *Itt;
129
		++Itt;
47 jab 130
		}
39 bj 131
*/
132
/*	for(int cI=0;cI<data.size();cI++)
133
		cout << data[cI] << "\t";
134
 
47 jab 135
		cout << endl << nCols << endl << nElems << endl;
39 bj 136
*/
137
 
47 jab 138
		}
39 bj 139
 
140
}