forked from MaxvandenBoom/matmef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matmef_dataconverter.c
1197 lines (951 loc) · 41 KB
/
matmef_dataconverter.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* Functions to convert primitive c-datatypes to matlab primitive (1x1) arrays/matrices and vice versa
*
* Copyright 2021, Max van den Boom
*
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "matmef_dataconverter.h"
#include "mex.h"
#include "mex_utils.h"
#include "meflib/meflib/meflib.h"
//
// Functions that create matlab-array(s) based on C data values/types
//
/**
* Create a (1x1 real) Uint8 matrix based on a MEF ui1 (unsigned 1 byte int) variable
*
* @param value The value to store in the matlab variable
* @return The mxArray containing the value
*/
mxArray *mxUint8ByValue(ui1 value) {
// create the matlab variable (1x1 real uint8 matrix) and retrieve the pointer to the allocated memory
mxArray *retArr = mxCreateNumericMatrix(1, 1, mxUINT8_CLASS, mxREAL);
mxUint8 *data = (mxUint8 *)mxGetData(retArr);
// transfer the value to the matlab (allocated memory)
*data = (mxUint8)value;
// return the matlab variable
return retArr;
}
/**
* Create a (1x1 real) Int8 matrix based on a MEF si1 (signed 1 byte int) variable
*
* @param value The value to store in the matlab variable
* @return The mxArray containing the value
*/
mxArray *mxInt8ByValue(si1 value) {
// create the matlab variable (1x1 real int8 matrix) and retrieve the pointer to the allocated memory
mxArray *retArr = mxCreateNumericMatrix(1, 1, mxINT8_CLASS, mxREAL);
mxInt8 *data = (mxInt8 *)mxGetData(retArr);
// transfer the value to the matlab (allocated memory)
*data = (mxInt8)value;
// return the matlab variable
return retArr;
}
/**
* Create a (1xN real) Uint8 vector/matrix based on a MEF array of ui1 (unsigned 1 byte int) values
* (contributed by Richard J. Cui, 4 apr 2020; updates by Max, 2020-2021)
*
* @param values The array of values to store in the matlab variable
* @param numBytes The number of values in the array to transfer
* @return The mxArray containing the array
*/
mxArray *mxUint8ArrayByValue(ui1 *values, int numBytes) {
int i;
// create the matlab variable (1x1 real double matrix)
mxArray *retArr = mxCreateNumericMatrix(1, numBytes, mxUINT8_CLASS, mxREAL);
// transfer the values to the matlab (allocated memory)
mxUint8 *data = (mxUint8 *)mxGetData(retArr);
for (i = 0; i < numBytes; i++) data[i] = values[i];
// return the matlab variable
return retArr;
}
/**
* Create a (1x1 real) Uint32 matrix based on a MEF ui4 (unsigned 4 byte int) variable
*
* @param value The value to store in the matlab variable
* @return The mxArray containing the value
*/
mxArray *mxUint32ByValue(ui4 value) {
// create the matlab variable (1x1 real uint32 matrix) and retrieve the pointer to the allocated memory
mxArray *retArr = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL);
mxUint32 *data = (mxUint32 *)mxGetData(retArr);
// transfer the value to the matlab (allocated memory)
*data = (mxUint32)value;
// return the matlab variable
return retArr;
}
/**
* Create a (1x1 real) Int32 matrix based on a MEF si4 (signed 4 byte int) variable
*
* @param value The value to store in the matlab variable
* @return The mxArray containing the value
*/
mxArray *mxInt32ByValue(si4 value) {
// create the matlab variable (1x1 real int32 matrix) and retrieve the pointer to the allocated memory
mxArray *retArr = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL);
mxInt32 *data = (mxInt32 *)mxGetData(retArr);
// transfer the value to the matlab (allocated memory)
*data = (mxInt32)value;
// return the matlab variable
return retArr;
}
/**
* Create a (1x1 real) Uint64 matrix based on a MEF ui8 (unsigned 8 byte int) variable
*
* @param value The value to store in the matlab variable
* @return The mxArray containing the value
*/
mxArray *mxUint64ByValue(ui8 value) {
// create the matlab variable (1x1 real uint64 matrix) and retrieve the pointer to the allocated memory
mxArray *retArr = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
mxUint64 *data = (mxUint64 *)mxGetData(retArr);
// transfer the value to the matlab (allocated memory)
*data = (mxUint64)value;
// return the matlab variable
return retArr;
}
/**
* Create a (1x1 real) Int64 matrix based on a MEF si8 (signed 8 byte int) variable
*
* @param value The value to store in the matlab variable
* @return The mxArray containing the value
*/
mxArray *mxInt64ByValue(si8 value) {
// create the matlab variable (1x1 real int64 matrix) and retrieve the pointer to the allocated memory
mxArray *retArr = mxCreateNumericMatrix(1, 1, mxINT64_CLASS, mxREAL);
mxInt64 *data = (mxInt64 *)mxGetData(retArr);
// transfer the value to the matlab (allocated memory)
*data = (mxInt64)value;
// return the matlab variable
return retArr;
}
/**
* Create a (1x1 real) double matrix based on a MEF sf4 (signed 4 byte float) variable
*
* @param value The value to store in the matlab variable
* @return The mxArray containing the value
*/
mxArray *mxSingleByValue(sf4 value) {
// create the matlab variable (1x1 real single matrix) and retrieve the pointer to the allocated memory
mxArray *retArr = mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS, mxREAL);
mxSingle *data = (mxSingle *)mxGetData(retArr);
// transfer the value to the matlab (allocated memory)
*data = (mxSingle)value;
// return the matlab variable
return retArr;
}
/**
* Create a (1x1 real) double matrix based on a MEF sf8 (signed 8 byte float) variable
*
* @param value The value to store in the matlab variable
* @return The mxArray containing the value
*/
mxArray *mxDoubleByValue(sf8 value) {
// create the matlab variable (1x1 real double matrix) and retrieve the pointer to the allocated memory
mxArray *retArr = mxCreateDoubleMatrix(1, 1, mxREAL);
mxDouble *data = (mxDouble *)mxGetData(retArr);
// transfer the value to the matlab (allocated memory)
*data = (mxDouble)value;
// return the matlab variable
return retArr;
}
/**
* Create a matlab char array based on a MEF UTF-8 character string
* (force/indicate UTF-8 encoding; depending on the OS locale matlab might be stuck on windows-1252)
*
* Note: Make sure the given char array pointer has a NULL-character at the end, else 'strlen' might crash
*
* @param str The char array to store in the matlab variable
* @return The mxArray containing the char array
*/
mxArray *mxStringByUtf8CharString(char *str) {
if (str == NULL) return mxCreateString("");
// retrieve the number of bytes in the (UTF-8) input string
// note: this might be differt then the actual number of characters
// (depending on the occurance of non-ASCII characters)
mwSize lengthInBytes = strlen(str);
// copy the byte values from the input string into an matlab uint8 array
// note: mxCreateString would do this, but also would destroy the non-ASCII
// code points (any byte with a value above 127 is converted to 65535)
mxArray *mat_uint8 = mxCreateNumericMatrix(1, lengthInBytes, mxUINT8_CLASS, mxREAL);
mxUint8 *p_mat_uint8 = (mxUint8 *)mxGetData(mat_uint8);
for (int i = 0; i < lengthInBytes; i++) {
p_mat_uint8[i] = str[i];
}
// use the native2unicode call from matlab to convert the
// UTF-8 bytes to a UTF-8 encoded char array in matlab
mxArray *lhs[1];
mxArray *rhs[] = {mat_uint8, mxCreateString("UTF-8")};
mexCallMATLAB(1, lhs, 2, rhs, "native2unicode");
// free the memory
mxDestroyArray(rhs[1]);
mxDestroyArray(mat_uint8);
// return matlab array (should now be a matlab UTF-8 string)
return lhs[0];
}
//
// Functions that copy data from a matlab-array to C data-types
//
/**
* Check whether the input matrix is numeric with a single value
*
* @param mat The matlab array to check
* @return True if the matrix has a single numeric value, false elsewise
*/
bool checkSingleNumericValue(const mxArray *mat) {
if (mxIsEmpty(mat) || !mxIsNumeric(mat) || mxGetNumberOfElements(mat) > 1) {
mexPrintf("Error: invalid input matrix, cannot transfer the matlab-array value to a C-variable (input is empty, not numeric or has more than 1 element), exiting...\n");
return false;
}
return true;
}
/**
* Copy a matlab (1x1 real) Uint8 matrix value to an existing MEF ui1 (unsigned 1 byte int) variable
*
* @param mat The source matlab array
* @param var A pointer to the destination MEF ui1 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxUint8ToVar(const mxArray *mat, ui1 *var) {
// check the input
if (!checkSingleNumericValue(mat))
return false;
if (mxGetClassID(mat) != mxUINT8_CLASS) {
mexPrintf("Error: could not copy the matlab-array value to a C-variable (input is not of the 'uint8' data-type), exiting...\n");
return false;
}
// copy and return success
mxUint8 *mat_data = (mxUint8 *)mxGetData(mat);
*var = *mat_data;
return true;
}
/**
* Copy a matlab (1x1 real) Int8 matrix value to an existing MEF si1 (signed 1 byte int) variable
*
* @param mat The source matlab array
* @param var A pointer to the destination MEF si1 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxInt8ToVar(const mxArray *mat, si1 *var) {
// check the input
if (!checkSingleNumericValue(mat))
return false;
if (mxGetClassID(mat) != mxINT8_CLASS) {
mexPrintf("Error: could not copy the matlab-array value to a C-variable (input is not of the 'int8' data-type), exiting...\n");
return false;
}
// copy and return success
mxInt8 *mat_data = (mxInt8 *)mxGetData(mat);
*var = *mat_data;
return true;
}
/**
* Copy the values from a matlab (1x1 real) Uint8 vector/matrix into an existing MEF ui1 (unsigned 1 byte int) array variable
*
* @param mat The source matlab array
* @param var A pointer to the destination MEF ui1 array variable
* @param varBytes The number of values that the destination MEF ui1 array variable can hold
* @return True if successfully tranferred, false on error
*/
bool cpyMxUint8ArrayToVar(const mxArray *mat, ui1 *var, int varBytes) {
// check the input
if (mxIsEmpty(mat) || !mxIsNumeric(mat) || mxGetNumberOfElements(mat) < 1) {
mexPrintf("Error: invalid input matrix, cannot transfer the matlab-array values into a C-array (input is empty, not numeric or does not have more any elements), exiting...\n");
return false;
}
if (mxGetClassID(mat) != mxUINT8_CLASS) {
mexPrintf("Error: could not copy the matlab-array values into a C-array (input is not of the 'uint8' data-type), exiting...\n");
return false;
}
// check if the number of elements matches
size_t numElements = mxGetNumberOfElements(mat);
if (numElements != varBytes) {
mexPrintf("Error: could not copy the matlab-array values into a C-array (the input array should be the same size as the C-array; the input has %i values, while the C-array is %i bytes), exiting...\n", numElements, varBytes);
return false;
}
// copy and return success
mxUint8 *mat_data = (mxUint8 *)mxGetData(mat);
for (int i = 0; i < numElements; i++)
var[i] = mat_data[i];
return true;
}
/**
* Copy a matlab (1x1 real) Uint32 matrix value to an existing MEF ui4 (unsigned 4 byte int) variable
*
* @param mat The source matlab array
* @param var A pointer to the destination MEF ui4 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxUint32ToVar(const mxArray *mat, ui4 *var) {
// check the input
if (!checkSingleNumericValue(mat))
return false;
if (mxGetClassID(mat) != mxUINT32_CLASS) {
mexPrintf("Error: could not copy the matlab-array value to a C-variable (input is not of the 'uint32' data-type), exiting...\n");
return false;
}
// copy and return success
mxUint32 *mat_data = (mxUint32 *)mxGetData(mat);
*var = *mat_data;
return true;
}
/**
* Copy a matlab (1x1 real) Int32 matrix value to an existing MEF si4 (signed 4 byte int) variable
*
* @param mat The source matlab array
* @param var A pointer to the destination MEF si4 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxInt32ToVar(const mxArray *mat, si4 *var) {
// check the input
if (!checkSingleNumericValue(mat))
return false;
if (mxGetClassID(mat) != mxINT32_CLASS) {
mexPrintf("Error: could not copy the matlab-array value to a C-variable (input is not of the 'int32' data-type), exiting...\n");
return false;
}
// copy and return success
mxInt32 *mat_data = (mxInt32 *)mxGetData(mat);
*var = *mat_data;
return true;
}
/**
* Copy a matlab (1x1 real) Uint64 matrix value to an existing MEF ui8 (unsigned 8 byte int) variable
*
* @param mat The source matlab array
* @param var A pointer to the destination MEF ui8 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxUint64ToVar(const mxArray *mat, ui8 *var) {
// check the input
if (!checkSingleNumericValue(mat))
return false;
if (mxGetClassID(mat) != mxUINT64_CLASS) {
mexPrintf("Error: could not copy the matlab-array value to a C-variable (input is not of the 'uint64' data-type), exiting...\n");
return false;
}
// copy and return success
mxUint64 *mat_data = (mxUint64 *)mxGetData(mat);
*var = *mat_data;
return true;
}
/**
* Copy a matlab (1x1 real) Int64 matrix value to an existing MEF si8 (signed 8 byte int) variable
*
* @param mat The source matlab array
* @param var A pointer to the destination MEF si8 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxInt64ToVar(const mxArray *mat, si8 *var) {
// check the input
if (!checkSingleNumericValue(mat))
return false;
if (mxGetClassID(mat) != mxINT64_CLASS) {
mexPrintf("Error: could not copy the matlab-array value to a C-variable (input is not of the 'int64' data-type), exiting...\n");
return false;
}
// copy and return success
mxInt64 *mat_data = (mxInt64 *)mxGetData(mat);
*var = *mat_data;
return true;
}
/**
* Copy a matlab (1x1 real) single float matrix value to an existing MEF sf4 (signed 4 byte float) variable
*
* @param mat The source matlab array
* @param var A pointer to the destination MEF sf4 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxSingleToVar(const mxArray *mat, sf4 *var) {
// check the input
if (!checkSingleNumericValue(mat))
return false;
if (mxGetClassID(mat) != mxSINGLE_CLASS) {
mexPrintf("Error: could not copy the matlab-array value to a C-variable (input is not of the 'single' data-type), exiting...\n");
return false;
}
// copy and return success
mxSingle *mat_data = (mxSingle *)mxGetData(mat);
*var = *mat_data;
return true;
}
/**
* Copy a matlab (1x1 real) double float matrix value to an existing MEF sf8 (signed 8 byte float) variable
*
* @param mat The source matlab array
* @param var A pointer to the destination MEF sf8 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxDoubleToVar(const mxArray *mat, sf8 *var) {
// check the input
if (!checkSingleNumericValue(mat))
return false;
if (mxGetClassID(mat) != mxDOUBLE_CLASS) {
mexPrintf("Error: could not copy the matlab-array value to a C-variable (input is not of the 'double' data-type), exiting...\n");
return false;
}
// copy and return success
mxDouble *mat_data = (mxDouble *)mxGetData(mat);
*var = *mat_data;
return true;
}
/**
* Copy a matlab char array to an existing MEF UTF-8 character string with a maximum size
*
* @param mat The source matlab char array
* @param str A pointer to the destination MEF UTF-8 character string
* @param strSize The (maximum) size of the destination MEF UTF-8 character string in bytes
* @return True if successfully tranferred, false on error
*/
bool cpyMxStringToUtf8CharString(const mxArray *mat, char *str, int strSize) {
// check the input
if (mxGetClassID(mat) != mxCHAR_CLASS) {
mexPrintf("Error: could not copy the matlab-array character string to C data-type (input is not of the 'char' data-type), exiting...\n");
return false;
}
// convert the char array in matlab to UTF-8 bytes
char *p_mat_uint8 = mxArrayToUTF8String(mat);
if (p_mat_uint8 == NULL) {
mexPrintf("Error: could not convert matlab char-array to UTF-8 bytes (input is most likely not a char-array), exiting...\n");
return false;
}
// retrieve the number of bytes in the UTF-8 input string
// note: this might be differt then the actual number of characters
// (depending on the occurance of non-ASCII characters)
mwSize lengthInBytes = strlen(p_mat_uint8);
// check maximum number of bytes
if (lengthInBytes + 1 > strSize) {
mexPrintf("Error: char array too large (length in UTF-8 bytes plus a NULL character: %i), variable can only hold %i bytes\n", lengthInBytes + 1, strSize);
mxFree(p_mat_uint8);
return false;
}
// transfer the string
for (mwSize i = 0; i < lengthInBytes; i++)
str[i] = p_mat_uint8[i];
// add a null character
str[lengthInBytes] = '\0';
// free the memory
mxFree(p_mat_uint8);
// return success
return true;
}
//
// Functions that copy data from a matlab-struct field to C data-types
//
/**
* Retrieve a matlab field from a struct-matrix
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to retrieve
* @return The matlab matrix in the field if successful, NULL on error
*/
mxArray *retrieveMxField(const mxArray *pm, const char *fieldname) {
mxArray *field = mxGetField(pm, 0, fieldname);
if (field == NULL)
mexPrintf("Error: could not map field '%s', field does not exist in the structure\n", fieldname);
return field;
}
/**
* Copy the value from a matlab field (being a 1x1 real, Uint8 matrix) in a struct-matrix to an existing MEF ui1 (unsigned 1 byte int) variable
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to copy the value from
* @param var A pointer to the destination MEF ui1 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxFieldUint8ToVar(const mxArray *pm, const char *fieldname, ui1 *var) {
// retrieve the field
const mxArray *field = retrieveMxField(pm, fieldname);
if (field == NULL) return false;
// copy the value
if (!cpyMxUint8ToVar(field, var)) {
mexPrintf("Error: could not copy the value from struct-field '%s', exiting...\n", fieldname);
return false;
}
// return success
return true;
}
/**
* Copy the value from a matlab field (being a 1x1 real, Int8 matrix) in a struct-matrix to an existing MEF si1 (signed 1 byte int) variable
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to copy the value from
* @param var A pointer to the destination MEF si1 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxFieldInt8ToVar(const mxArray *pm, const char *fieldname, si1 *var) {
// retrieve the field
const mxArray *field = retrieveMxField(pm, fieldname);
if (field == NULL) return false;
// copy the value
if (!cpyMxInt8ToVar(field, var)) {
mexPrintf("Error: could not copy the value from struct-field '%s', exiting...\n", fieldname);
return false;
}
// return success
return true;
}
/**
* Copy the values from a matlab field (being a 1x1 real, Uint8 vector/matrix) in a struct-matrix into an existing MEF ui1 (unsigned 1 byte int) array variable
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to copy the values from
* @param var A pointer to the destination MEF ui1 array variable
* @param varBytes The number of values that the destination MEF ui1 array variable can hold
* @return True if successfully tranferred, false on error
*/
bool cpyMxFieldUint8ArrayToVar(const mxArray *pm, const char *fieldname, ui1 *var, int varBytes) {
// retrieve the field
const mxArray *field = retrieveMxField(pm, fieldname);
if (field == NULL) return false;
// copy the values
if (!cpyMxUint8ArrayToVar(field, var, varBytes)) {
mexPrintf("Error: could not copy the values from struct-field '%s', exiting...\n", fieldname);
return false;
}
// return success
return true;
}
/**
* Copy the value from a matlab field (being a 1x1 real, Uint32 matrix) in a struct-matrix to an existing MEF ui4 (unsigned 4 byte int) variable
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to copy the value from
* @param var A pointer to the destination MEF ui4 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxFieldUint32ToVar(const mxArray *pm, const char *fieldname, ui4 *var) {
// retrieve the field
const mxArray *field = retrieveMxField(pm, fieldname);
if (field == NULL) return false;
// copy the value
if (!cpyMxUint32ToVar(field, var)) {
mexPrintf("Error: could not copy the value from struct-field '%s', exiting...\n", fieldname);
return false;
}
// return success
return true;
}
/**
* Copy the value from a matlab field (being a 1x1 real, Int32 matrix) in a struct-matrix to an existing MEF si4 (signed 4 byte int) variable
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to copy the value from
* @param var A pointer to the destination MEF si4 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxFieldInt32ToVar(const mxArray *pm, const char *fieldname, si4 *var) {
// retrieve the field
const mxArray *field = retrieveMxField(pm, fieldname);
if (field == NULL) return false;
// copy the value
if (!cpyMxInt32ToVar(field, var)) {
mexPrintf("Error: could not copy the value from struct-field '%s', exiting...\n", fieldname);
return false;
}
// return success
return true;
}
/**
* Copy the value from a matlab field (being a 1x1 real, UInt64 matrix) in a struct-matrix to an existing MEF ui8 (unsigned 8 byte int) variable
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to copy the value from
* @param var A pointer to the destination MEF ui8 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxFieldUint64ToVar(const mxArray *pm, const char *fieldname, ui8 *var) {
// retrieve the field
const mxArray *field = retrieveMxField(pm, fieldname);
if (field == NULL) return false;
// copy the value
if (!cpyMxUint64ToVar(field, var)) {
mexPrintf("Error: could not copy the value from struct-field '%s', exiting...\n", fieldname);
return false;
}
// return success
return true;
}
/**
* Copy the value from a matlab field (being a 1x1 real, Int64 matrix) in a struct-matrix to an existing MEF si8 (signed 8 byte int) variable
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to copy the value from
* @param var A pointer to the destination MEF si8 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxFieldInt64ToVar(const mxArray *pm, const char *fieldname, si8 *var) {
// retrieve the field
const mxArray *field = retrieveMxField(pm, fieldname);
if (field == NULL) return false;
// copy the value
if (!cpyMxInt64ToVar(field, var)) {
mexPrintf("Error: could not copy the value from struct-field '%s', exiting...\n", fieldname);
return false;
}
// return success
return true;
}
/**
* Copy the value from a matlab field (being a 1x1 real, single float matrix) in a struct-matrix to an existing MEF sf4 (signed 4 byte float) variable
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to copy the value from
* @param var A pointer to the destination MEF sf4 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxFieldSingleToVar(const mxArray *pm, const char *fieldname, sf4 *var) {
// retrieve the field
const mxArray *field = retrieveMxField(pm, fieldname);
if (field == NULL) return false;
// copy the value
if (!cpyMxSingleToVar(field, var)) {
mexPrintf("Error: could not copy the value from struct-field '%s', exiting...\n", fieldname);
return false;
}
// return success
return true;
}
/**
* Copy the value from a matlab field (being a 1x1 real, double float matrix) in a struct-matrix to an existing MEF sf8 (signed 8 byte float) variable
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to copy the value from
* @param var A pointer to the destination MEF sf8 variable
* @return True if successfully tranferred, false on error
*/
bool cpyMxFieldDoubleToVar(const mxArray *pm, const char *fieldname, sf8 *var) {
// retrieve the field
const mxArray *field = retrieveMxField(pm, fieldname);
if (field == NULL) return false;
// copy the value
if (!cpyMxDoubleToVar(field, var)) {
mexPrintf("Error: could not copy the value from struct-field '%s', exiting...\n", fieldname);
return false;
}
// return success
return true;
}
/**
* Copy the value from a matlab field (being a char array) in a struct-matrix to an existing MEF UTF-8 character string with a maximum size
*
* @param pm Pointer to the matlab struct-matrix that holds the field
* @param fieldname The name of the field in the matlab struct-matrix to copy the value from
* @param str A pointer to the destination MEF UTF-8 character string
* @param strSize The (maximum) size of the destination MEF UTF-8 character string in bytes
* @return True if successfully tranferred, false on error
*/
bool cpyMxFieldStringToUtf8CharString(const mxArray *pm, const char *fieldname, char *str, int strSize) {
// retrieve the field
const mxArray *field = retrieveMxField(pm, fieldname);
if (field == NULL) return false;
// copy the UTF-8 string
if (!cpyMxStringToUtf8CharString(field, str, strSize)) {
mexPrintf("Error: could not copy the value from struct-field '%s', exiting...\n", fieldname);
return false;
}
// return success
return true;
}
//
// Functions that check and convert a matlab input argument matrix with a single value
//
/**
* Check and extract a boolean value from a matlab input argument matrix
*
* @param mat The matlab array with a single value to check and extract
* @param argName The name of the input argument (used in error messages)
* @param pVar Pointer to a boolean variable to store the value in
* @return True if a valid single numeric boolean value was extracted, false elsewise
*/
bool getInputArgAsBool(const mxArray *mat, const char *argName, bool *pVar) {
// check the input matrix
if (mxIsEmpty(mat)) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is empty, should be 0 (false) or 1 (true)", argName);
return false;
}
if (!mxIsLogicalScalar(mat) && !mxIsNumeric(mat)) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is not numeric or logical, should be 0 (false) or 1 (true)", argName);
return false;
}
if (mxGetNumberOfElements(mat) != 1) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument invalid, should be a single numeric or logical value", argName);
return false;
}
// assign the value and return succes
*pVar = (mxIsLogicalScalarTrue(mat) || mxGetScalar(mat) == 1);
return true;
}
/**
* Check and extract a single MEF si8 (signed 8 byte int) value from a matlab input argument matrix
*
* @param mat The matlab array with a single value to check and extract
* @param argName The name of the input argument (used in error messages)
* @param minValue The minimum integer value (also used to determine whether the double data-type can be used)
* @param maxValue The maximum integer value (also used to determine whether the double data-type can be used)
* @param pVar Pointer to a si8 variable to store the value in
* @return True if a valid single numeric si8 value was extracted, false elsewise
*/
bool getInputArgAsInt64(const mxArray *mat, const char *argName, si8 minValue, si8 maxValue, si8 *pVar) {
// check the input matrix
if (mxIsEmpty(mat)) {
if (minValue == -1)
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is empty, should be -1, 0 or a positive integer (1, 2, ...)", argName);
else
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is empty, should be a numeric value", argName);
return false;
}
if (!mxIsNumeric(mat)) {
if (minValue == -1)
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is not numeric, should be -1, 0 or a positive integer (1, 2, ...)", argName);
else
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is not numeric", argName);
return false;
}
if (mxGetNumberOfElements(mat) != 1) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument invalid, should be a single numeric value", argName);
return false;
}
// try to retrieve the value as an si8
si8 mat_si8 = 0;
mxClassID classID = mxGetClassID(mat);
if (classID == mxINT64_CLASS) {
// same type, just use the value
mat_si8 = (si8)*(mxInt64 *)mxGetData(mat);
} else if (classID == mxDOUBLE_CLASS) {
// retrieve the value as a scalar first
double dbl_mat = mxGetScalar(mat);
// check if we only allow a negative value of -1 and the value is indeed -1
// (if the value that is passed is -1 then there is no risk of imprecision)
if (minValue == -1 && dbl_mat == -1) {
// allow only -1 and value is -1
mat_si8 = -1;
} else {
// elsewise
// check if the value that we are trying to retrieve from the double can is smaller or
// larger than what a double variable can contain without losing precision (<= 2^53; so <= 9007199254740992)
if (classID == mxDOUBLE_CLASS && (dbl_mat < -9007199254740992 || dbl_mat > 9007199254740992)) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument data-type is invalid. The argument is a double; because the argument might require a value smaller than -2^53 or larger than 2^53, using a double for input could result in a loss of precision. Instead pass the value as an signed 64-bit integer (e.g. 'int64(1024)')", argName);
return false;
}
// check the scalar
if (mxIsNaN(dbl_mat) || mxIsInf(dbl_mat)) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is NaN or Inf, should be an integer", argName);
return false;
}
if (floor(dbl_mat) != dbl_mat) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is a fraction, should be an integer", argName);
return false;
}
// transfer to si8 variable
mat_si8 = (si8)dbl_mat;
}
} else {
if (minValue < -9007199254740992 || maxValue > 9007199254740992)
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument data-type (%s) is invalid, should be int64", argName, mxGetClassName(mat));
else
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument data-type (%s) is invalid, should be an int64 or double", argName, mxGetClassName(mat));
return false;
}
// check the value
if (mat_si8 < minValue) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is invalid, the numeric value cannot be smaller than %lld", argName, minValue);
return false;
}
if (mat_si8 > maxValue) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is invalid, the numeric value cannot be greater than %lld", argName, maxValue);
return false;
}
// assign the value and return succes
*pVar = mat_si8;
return true;
}
/**
* Check and extract a single MEF ui8 (unsigned 8 byte int) value from a matlab input argument matrix
*
* @param mat The matlab array with a single value to check and extract
* @param argName The name of the input argument (used in error messages)
* @param maxValue The maximum integer value (also used to determine whether the double data-type can be used)
* @param pVar Pointer to a ui8 variable to store the value in
* @return True if a valid single numeric si8 value was extracted, false elsewise
*/
bool getInputArgAsUint64(const mxArray *mat, const char *argName, ui8 maxValue, ui8 *pVar) {
// check the input matrix
if (mxIsEmpty(mat)) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is empty", argName);
return false;
}
if (!mxIsNumeric(mat)) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument is not numeric, should be 0 or a positive integer (1, 2, ...)", argName);
return false;
}
if (mxGetNumberOfElements(mat) != 1) {
mexErrMsgIdAndTxt("MATLAB:matmef_utils:invalidArg", "'%s' input argument invalid, should be a single numeric value", argName);
return false;
}
// try to retrieve the value as an ui8
ui8 mat_ui8 = 0;
mxClassID classID = mxGetClassID(mat);
if (classID == mxUINT64_CLASS) {
// same type, just use the value
mat_ui8 = (ui8)*(mxUint64 *)mxGetData(mat);
} else if (classID == mxDOUBLE_CLASS) {