-
Notifications
You must be signed in to change notification settings - Fork 0
/
mexLoadAgilentParams.c
168 lines (151 loc) · 3.97 KB
/
mexLoadAgilentParams.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright 2010-2014 Regents of the University of Minnesota.
* This file subject to terms of Creative Commons CC BY-NC 4.0, see LICENSE.html
* Copyright 2012 Michael Tesch. All rights reserved.
*
* author(s): Michael Tesch ([email protected]),
*/
/*! \file
* \brief Load agilent parameter file into a struct
*
* \section Building
*
* Use mtmatpack_setup.m or mtagipack_setup.m
*
* \section Example
\verbatim
octave:2> mexLoadAgilentParams
error: mexLoadAgilentParams: usage: paramstruct = mexLoadAgilentParams(parfile)
octave:2> params = mexLoadAgilentParams('/Users/tesch/Documents/umn/data/4T/rcswift_normal_01.fid/procpar');
octave:4> params.nv
ans = 4096
octave:5> params.sw
ans = 8.3333e+05
octave:7> params.saveglobal_
ans =
{
[1,1] = probe
[2,1] = lcpeak
[3,1] = loc
[4,1] = lockpower
[5,1] = lockgain
[6,1] = lockphase
[7,1] = lockfreq
[8,1] = z0
[9,1] = lkof
[10,1] = vloc
[11,1] = vrack
[12,1] = vzone
[13,1] = vproto
[14,1] = pkpick
[15,1] = parstyle
[16,1] = operator
[17,1] = studyid
[18,1] = systemname
[19,1] = probetype
}
octave:8> params.saveglobal_{8}
ans = z0
octave:9>
octave:9> params.dx
ans =
0
1
1
0
-1
-1
0
octave:10> params.dx(5)
ans = -1
\endverbatim
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "premexh.h"
#include "mex.h"
#include "cprocpar.h"
#ifdef DOXYGEN
#define mexFunction mexLoadAgilentParams
#endif
/* paramstruct = mexLoadAgilentParams(filename) */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char measfile[512];
char * errmsg =
"usage: paramstruct = mexLoadAgilentParams(parfile)\n";
procpar_t * pp;
int count;
int i;
const char ** keys;
mwSize dims[2] = {1, 1};
if (nrhs != 1 || nlhs != 1 || !mxIsChar(prhs[0])) {
mexWarnMsgTxt(" bad params");
mexErrMsgTxt(errmsg);
}
if (mxGetString(prhs[0], measfile, sizeof(measfile))) {
mexWarnMsgTxt(" can't get param file name from argument");
mexErrMsgTxt(errmsg);
}
/* read the procpar */
pp = pp_open(measfile, NULL, TREE_PROC);
if (!pp) {
mexWarnMsgTxt(measfile);
mexErrMsgTxt(":unable to open param file");
}
count = pp_size(pp);
/* gather param names into key array */
keys = malloc(sizeof(char *) * count);
if (!keys)
mexErrMsgTxt("malloc");
for (i = 0; i < count; i++) {
keys[i] = pp_get_next(pp);
}
/* Construct outdata */
plhs[0] = mxCreateStructArray(2, dims, count, keys);
/* set struct values */
for (i = 0; i < count; i++) {
mxArray *field_value = NULL; // compiler warning
int j, fieldcount;
fieldcount = pp_get_count(pp, keys[i]);
dims[0] = fieldcount;
switch (pp_get_type(pp, keys[i])) {
case VAR_INT:
field_value = mxCreateNumericArray(2, dims, mxINT32_CLASS, mxREAL);
for (j = 0; j < fieldcount; j++) {
double val;
if (pp_get_real(pp, keys[i], j, &val))
mexErrMsgTxt("mexLoadAgilentParams error in pp_get_real() (INT)");
((int *)mxGetPr(field_value))[j] = val;
}
break;
case VAR_REAL:
field_value = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
for (j = 0; j < fieldcount; j++) {
double val;
if (pp_get_real(pp, keys[i], j, &val))
mexErrMsgTxt("mexLoadAgilentParams error in pp_get_real()");
mxGetPr(field_value)[j] = val;
//printf("%s=%f\n", keys[i], pp_get_real(pp, keys[i], j));
}
break;
case VAR_STRING:
field_value = mxCreateCellMatrix(fieldcount, 1);
for (j = 0; j < fieldcount; j++) {
const char * strptr;
if (pp_get_string(pp, keys[i], j, &strptr))
mexErrMsgTxt("mexLoadAgilentParams error in pp_get_string()");
mxSetCell(field_value, j, mxCreateString(strptr));
}
break;
case VAR_NOEXIST:
default:
mexErrMsgTxt("mexLoadAgilentParams internal error");
}
mxSetFieldByNumber(plhs[0], 0, i, field_value);
}
//free(keys);
}