Generating MATLAB Code to Reproduce a Graph
Generating Matlab Code
Create a Stem Plot and Generate Code for It
Suppose you have created the following graph.
t = 0:.2:20; alpha =.055; stem(t,exp(-alpha*t).*sin(5*t))
Use the Property Editor to modify the graph. Select the stemseries and change the marker fill color to dark red, and marker edge color and line color to dark green. Remove the axes box, and change the font size for the axes labels to 8 to look like the following picture:
You can generate code to reproduce this graph by selecting Generate M-File from the Figure menu. MATLAB code generation software composes a function that recreates the graph and opens the generated file in the editor.
This feature is particularly useful for capturing property settings and other modifications you make using the plot tools GUI.
The file appears in an editor window and consists of the following code:
function createfigure(X1, Y1) %CREATEFIGURE(X1,Y1) % X1: stem x % Y1: stem y % Auto-generated by MATLAB on 24-May-2006 14:23:45 % Create figure figure1 = figure('Color',[1 1 1]); % Create axes axes('Parent',figure1,'FontSize',8); hold('all'); % Create stem stem(X1,Y1,'MarkerFaceColor',[0.8471 0.1608 0],... 'MarkerEdgeColor',[0.1686 0.5059 0.3373],... 'Color',[0 0.498 0]);
You must save the file before exiting MATLAB if you want to use it in future sessions.
Generated functions do not store the data necessary to recreate the graph. You must supply the data arguments t as X1 and exp(-alpha*t).*sin(5*t) as Y1 to the function to recreate your graph. Of course, you can call the generated function with other argument pairs too.
Attempting to generate code for graphs containing a large number of graphics objects (e.g., greater than 20 plotted lines) might be impractical.