1 //------------------------------------------------------------------------------
2 // gbtype: type of a GraphBLAS matrix struct, or any MATLAB variable
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: GPL-3.0-or-later
7 
8 //------------------------------------------------------------------------------
9 
10 // The input may be any MATLAB variable.  If it is a GraphBLAS G.opaque struct,
11 // then its internal type is returned.
12 
13 // Usage
14 
15 // type = gbtype (X)
16 
17 #include "gb_matlab.h"
18 
19 #define USAGE "usage: type = gbtype (X)"
20 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])21 void mexFunction
22 (
23     int nargout,
24     mxArray *pargout [ ],
25     int nargin,
26     const mxArray *pargin [ ]
27 )
28 {
29 
30     //--------------------------------------------------------------------------
31     // check inputs
32     //--------------------------------------------------------------------------
33 
34     gb_usage (nargin == 1 && nargout <= 1, USAGE) ;
35 
36     //--------------------------------------------------------------------------
37     // get the type of the matrix
38     //--------------------------------------------------------------------------
39 
40     mxArray *c = NULL ;
41     mxClassID class = mxGetClassID (pargin [0]) ;
42     bool is_complex = mxIsComplex (pargin [0]) ;
43 
44     if (class == mxSTRUCT_CLASS)
45     {
46         // get the content of a GraphBLASv5 struct
47         mxArray *mx_type = mxGetField (pargin [0], 0, "GraphBLASv5") ;
48         if (mx_type == NULL)
49         {
50             // check if it is a GraphBLASv4 struct
51             mx_type = mxGetField (pargin [0], 0, "GraphBLASv4") ;
52         }
53         if (mx_type == NULL)
54         {
55             // check if it is a GraphBLASv3 struct
56             mx_type = mxGetField (pargin [0], 0, "GraphBLAS") ;
57         }
58         if (mx_type != NULL)
59         {
60             // the matrix is a GraphBLAS v3, v4, or v5 struct; get its type
61             c = mxDuplicateArray (mx_type) ;
62         }
63     }
64 
65     if (c == NULL)
66     {
67         // if c is still NULL, then it is not a GraphBLAS opaque struct.
68         // get the type of a MATLAB matrix
69         c = gb_mxclass_to_mxstring (class, is_complex) ;
70     }
71 
72     //--------------------------------------------------------------------------
73     // return the result
74     //--------------------------------------------------------------------------
75 
76     pargout [0] = c ;
77     GB_WRAPUP ;
78 }
79 
80