-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
348 lines (285 loc) · 8.7 KB
/
example_test.go
File metadata and controls
348 lines (285 loc) · 8.7 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Package matlab_test provides testable examples for the MATLAB file library.
//
// These examples demonstrate common use cases and serve as both documentation
// and verification that the API works as expected.
package matlab_test
import (
"encoding/binary"
"fmt"
"os"
"path/filepath"
"github.com/scigolib/matlab"
"github.com/scigolib/matlab/types"
)
// Example demonstrates basic usage of the MATLAB file library.
func Example() {
tmpfile := filepath.Join(os.TempDir(), "example.mat")
defer os.Remove(tmpfile)
// Create a simple v5 MATLAB file
writer, _ := matlab.Create(tmpfile, matlab.Version5)
defer writer.Close()
// Write a variable
writer.WriteVariable(&types.Variable{
Name: "data",
Dimensions: []int{3},
DataType: types.Double,
Data: []float64{1.0, 2.0, 3.0},
})
fmt.Println("MATLAB file created successfully")
// Output:
// MATLAB file created successfully
}
// ExampleCreate demonstrates creating a MATLAB file.
func ExampleCreate() {
tmpfile := filepath.Join(os.TempDir(), "example_create.mat")
defer os.Remove(tmpfile)
writer, err := matlab.Create(tmpfile, matlab.Version5)
if err != nil {
panic(err)
}
defer writer.Close()
fmt.Println("File created")
// Output:
// File created
}
// ExampleCreate_v5 demonstrates creating a v5 format file.
func ExampleCreate_v5() {
tmpfile := filepath.Join(os.TempDir(), "example_v5.mat")
defer os.Remove(tmpfile)
writer, _ := matlab.Create(tmpfile, matlab.Version5)
defer writer.Close()
writer.WriteVariable(&types.Variable{
Name: "matrix",
Dimensions: []int{2, 3},
DataType: types.Double,
Data: []float64{1, 2, 3, 4, 5, 6},
})
fmt.Println("v5 file created")
// Output:
// v5 file created
}
// ExampleCreate_v73 demonstrates creating a v7.3 HDF5 format file.
func ExampleCreate_v73() {
tmpfile := filepath.Join(os.TempDir(), "example_v73.mat")
defer os.Remove(tmpfile)
writer, _ := matlab.Create(tmpfile, matlab.Version73)
defer writer.Close()
writer.WriteVariable(&types.Variable{
Name: "data",
Dimensions: []int{100},
DataType: types.Double,
Data: make([]float64, 100),
})
fmt.Println("v7.3 file created")
// Output:
// v7.3 file created
}
// ExampleOpen demonstrates reading a MATLAB file.
func ExampleOpen() {
file, _ := os.Open("testdata/generated/simple_double.mat")
defer file.Close()
matFile, _ := matlab.Open(file)
fmt.Printf("Found %d variable(s)\n", len(matFile.Variables))
// Output:
// Found 1 variable(s)
}
// ExampleMatFile_Variables demonstrates iterating over variables.
func ExampleMatFile_Variables() {
file, _ := os.Open("testdata/generated/simple_double.mat")
defer file.Close()
matFile, _ := matlab.Open(file)
for _, v := range matFile.Variables {
fmt.Printf("Variable: %s, Type: %v\n", v.Name, v.DataType)
}
// Output:
// Variable: data, Type: double
}
// ExampleMatFileWriter_WriteVariable demonstrates writing a simple array.
func ExampleMatFileWriter_WriteVariable() {
tmpfile := filepath.Join(os.TempDir(), "example_array.mat")
defer os.Remove(tmpfile)
writer, _ := matlab.Create(tmpfile, matlab.Version5)
defer writer.Close()
err := writer.WriteVariable(&types.Variable{
Name: "mydata",
Dimensions: []int{5},
DataType: types.Double,
Data: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
})
if err == nil {
fmt.Println("Variable written")
}
// Output:
// Variable written
}
// ExampleMatFileWriter_WriteVariable_matrix demonstrates writing a 2D matrix.
func ExampleMatFileWriter_WriteVariable_matrix() {
tmpfile := filepath.Join(os.TempDir(), "example_matrix.mat")
defer os.Remove(tmpfile)
writer, _ := matlab.Create(tmpfile, matlab.Version5)
defer writer.Close()
// 3x4 matrix in column-major order (MATLAB standard)
writer.WriteVariable(&types.Variable{
Name: "A",
Dimensions: []int{3, 4},
DataType: types.Double,
Data: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
})
fmt.Println("Matrix written")
// Output:
// Matrix written
}
// ExampleMatFileWriter_WriteVariable_complex demonstrates writing complex numbers.
func ExampleMatFileWriter_WriteVariable_complex() {
tmpfile := filepath.Join(os.TempDir(), "example_complex.mat")
defer os.Remove(tmpfile)
writer, _ := matlab.Create(tmpfile, matlab.Version73)
defer writer.Close()
complexData := &types.NumericArray{
Real: []float64{1.0, 2.0, 3.0},
Imag: []float64{4.0, 5.0, 6.0},
}
writer.WriteVariable(&types.Variable{
Name: "signal",
Dimensions: []int{3},
DataType: types.Double,
Data: complexData,
IsComplex: true,
})
fmt.Println("Complex variable written")
// Output:
// Complex variable written
}
// ExampleMatFileWriter_WriteVariable_int32 demonstrates writing integer data.
func ExampleMatFileWriter_WriteVariable_int32() {
tmpfile := filepath.Join(os.TempDir(), "example_integers.mat")
defer os.Remove(tmpfile)
writer, _ := matlab.Create(tmpfile, matlab.Version5)
defer writer.Close()
writer.WriteVariable(&types.Variable{
Name: "counts",
Dimensions: []int{4},
DataType: types.Int32,
Data: []int32{10, 20, 30, 40},
})
fmt.Println("Integer array written")
// Output:
// Integer array written
}
// ExampleVariable_Data_simple demonstrates accessing simple numeric data.
func ExampleVariable_Data_simple() {
file, _ := os.Open("testdata/generated/simple_double.mat")
defer file.Close()
matFile, _ := matlab.Open(file)
variable := matFile.Variables[0]
// Simple arrays are stored directly
data := variable.Data.([]float64)
fmt.Printf("First value: %.1f\n", data[0])
// Output:
// First value: 1.0
}
// ExampleVariable_Data_complex demonstrates the structure of complex number data.
func ExampleVariable_Data_complex() {
// Create a complex numeric array
complexData := &types.NumericArray{
Real: []float64{1, 2, 3},
Imag: []float64{4, 5, 6},
}
// Demonstrate accessing the parts
fmt.Printf("Real part: %v\n", complexData.Real)
fmt.Printf("Imag part: %v\n", complexData.Imag)
// Output:
// Real part: [1 2 3]
// Imag part: [4 5 6]
}
// ExampleOpen_roundTrip demonstrates writing and reading back data.
func ExampleOpen_roundTrip() {
tmpfile := filepath.Join(os.TempDir(), "example_roundtrip.mat")
defer os.Remove(tmpfile)
// Write
writer, _ := matlab.Create(tmpfile, matlab.Version5)
writer.WriteVariable(&types.Variable{
Name: "test",
Dimensions: []int{2},
DataType: types.Double,
Data: []float64{3.14, 2.71},
})
writer.Close()
// Read
file, _ := os.Open(tmpfile)
defer file.Close()
matFile, _ := matlab.Open(file)
data := matFile.Variables[0].Data.([]float64)
fmt.Printf("Read back: %.2f, %.2f\n", data[0], data[1])
// Output:
// Read back: 3.14, 2.71
}
// ExampleOpen_multipleVariables demonstrates handling multiple variables.
func ExampleOpen_multipleVariables() {
tmpfile := filepath.Join(os.TempDir(), "example_multi.mat")
defer os.Remove(tmpfile)
// Write multiple variables
writer, _ := matlab.Create(tmpfile, matlab.Version5)
writer.WriteVariable(&types.Variable{
Name: "x",
Dimensions: []int{3},
DataType: types.Double,
Data: []float64{1, 2, 3},
})
writer.WriteVariable(&types.Variable{
Name: "y",
Dimensions: []int{3},
DataType: types.Double,
Data: []float64{4, 5, 6},
})
writer.Close()
// Read all variables
file, _ := os.Open(tmpfile)
defer file.Close()
matFile, _ := matlab.Open(file)
fmt.Printf("Total variables: %d\n", len(matFile.Variables))
for _, v := range matFile.Variables {
fmt.Printf("- %s\n", v.Name)
}
// Output:
// Total variables: 2
// - x
// - y
}
// ExampleCreate_withOptions demonstrates using functional options.
func ExampleCreate_withOptions() {
tmpfile := filepath.Join(os.TempDir(), "options.mat")
defer os.Remove(tmpfile)
writer, _ := matlab.Create(tmpfile, matlab.Version5,
matlab.WithEndianness(binary.BigEndian),
matlab.WithDescription("Simulation results"),
)
defer writer.Close()
fmt.Println("File created with custom options")
// Output:
// File created with custom options
}
// ExampleWithEndianness demonstrates setting byte order.
func ExampleWithEndianness() {
tmpfile := filepath.Join(os.TempDir(), "bigendian.mat")
defer os.Remove(tmpfile)
writer, _ := matlab.Create(tmpfile, matlab.Version5,
matlab.WithEndianness(binary.BigEndian),
)
defer writer.Close()
fmt.Println("Big-endian file created")
// Output:
// Big-endian file created
}
// ExampleWithDescription demonstrates custom file description.
func ExampleWithDescription() {
tmpfile := filepath.Join(os.TempDir(), "described.mat")
defer os.Remove(tmpfile)
writer, _ := matlab.Create(tmpfile, matlab.Version5,
matlab.WithDescription("My experimental data from 2025"),
)
defer writer.Close()
fmt.Println("File with custom description created")
// Output:
// File with custom description created
}