Create and Modify a MATLAB Figure

Preparing a MATLAB Figure for Export

  1. Create a figure window. For example:

    h = figure;
  2. Add graphics to the figure. For example:

    surf(peaks);

Changing the Figure (Optional)

Optionally, you can change the figure numerous ways. For example:

Alter Visibility

 set(h, 'Visible', 'off');

Change Background Color

 set(h, 'Color', [.8,.9,1]);

Alter Orientation and Size

width=500;
height=500;
rotation=30;
elevation=30;
set(h, 'Position', [0, 0, width, height]);
view([rotation, elevation]);

Exporting the Figure

Export the contents of the figure in one of two ways:

WebFigure

To export as a WebFigure:

returnFigure = webfigure(h);    

Image Data

To export image data, for example:

imgform = 'png';
returnByteArray = figToImStream(`figHandle', h, ...
                 `imageFormat', imgform, ...
                 `outputType', `uint8');  

Cleaning Up the Figure Window

To close the figure window:

close(h);

Modify and Export Figure Data

WebFigure

        function returnFigure = getWebFigure()
        h = figure;
        set(h, 'Visible', 'off');
        surf(peaks);
        set(h, 'Color', [.8,.9,1]);
        returnFigure = webfigure(h);    
        close(h);

Image Data

function returnByteArray = getImageDataOrientation(height,
                 width, elevation, rotation, imageFormat )
h = figure;
set(h, 'Visible', 'off');
surf(peaks);
set(h, 'Color', [.8,.9,1]);
set(h, 'Position', [0, 0, width, height]);
view([rotation, elevation]);
returnByteArray = figToImStream(`figHandle', h, ...
                                `imageFormat', imageFormat, ...
                                `outputType', `uint8');  
close(h);

function returnByteArray = getImageDataOrientation(height,
                 width, elevation, rotation, imageFormat )
h = figure;
set(h, 'Visible', 'off');
surf(peaks);
set(h, 'Color', [.8,.9,1]);
set(h, 'Position', [0, 0, width, height]);
view([rotation, elevation]);
returnByteArray = figToImStream(`figHandle', h, ...
                                `imageFormat', imageFormat, ...
                                `outputType', `int8');  
close(h);

Was this topic helpful?