MATLAB: Converting RGB surf plot to stl file for 3D printing

3d printingimage processingstlsurf

I'm currently writing a script that takes a 2d image and turns it into a 3d stl file for 3D printing. So far I have: im = imread(filename); size(im)
im_red = im(:,:,1); size(im_red)
h=surf(double(im_red))
From here Matlab plots the image according to RGB data, which creates a 3D image. I would like to take this surf plot and convert it into an stl file.
Thank you!