1 //------------------------------------------------------------------------------
2 // gbmatlab: convert to a sparse or full MATLAB matrix
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 either a GraphBLAS matrix struct or a standard MATLAB
11 // sparse or full matrix. The output is a standard MATLAB sparse or full
12 // matrix: full if all entries are present, and sparse otherwise.
13
14 // Usage:
15
16 // A = gbmatlab (X, type)
17
18 #include "gb_matlab.h"
19
20 #define USAGE "usage: A = gbmatlab (X, type)"
21
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])22 void mexFunction
23 (
24 int nargout,
25 mxArray *pargout [ ],
26 int nargin,
27 const mxArray *pargin [ ]
28 )
29 {
30
31 //--------------------------------------------------------------------------
32 // check inputs
33 //--------------------------------------------------------------------------
34
35 gb_usage (nargin == 2 && nargout <= 1, USAGE) ;
36
37 //--------------------------------------------------------------------------
38 // get the input matrix
39 //--------------------------------------------------------------------------
40
41 GrB_Matrix X = gb_get_shallow (pargin [0]) ;
42 GrB_Type xtype ;
43 OK (GxB_Matrix_type (&xtype, X)) ;
44
45 //--------------------------------------------------------------------------
46 // get the desired type, and typecast if needed
47 //--------------------------------------------------------------------------
48
49 GrB_Type type = gb_mxstring_to_type (pargin [1]) ;
50 GrB_Matrix T = NULL ;
51 if (type != xtype)
52 {
53 T = gb_typecast (X, type, GxB_BY_COL, GxB_SPARSE + GxB_FULL) ;
54 OK (GrB_Matrix_free (&X)) ;
55 X = T ;
56 }
57
58 //--------------------------------------------------------------------------
59 // export the input matrix to a MATLAB sparse or full matrix
60 //--------------------------------------------------------------------------
61
62 pargout [0] = gb_export (&X, KIND_MATLAB) ;
63 GB_WRAPUP ;
64 }
65
66