MATLAB | 怎么让MATLAB自己生成代码?

Posted slandarer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MATLAB | 怎么让MATLAB自己生成代码?相关的知识,希望对你有一定的参考价值。

国赛就是明天了,写了个很有意思的东西,虽然目前只是个雏形但是,这玩意真的挺有意思的,很多人写完基础代码绘图后不咋修饰就点开属性编辑器一顿点点点,但这样画出的图毕竟只有图没有相关代码,有没有啥办法能够把点点点的过程转化为代码,能把论文写的更长一点?于是就有了下面这个小工具,首先介绍用法,最后给出工具函数完整代码(初代懒得写注释ing):

基本使用

写完绘图函数后在代码最后加入一行codeprinter

t=0.01:0.2:3*pi;
hold on
plot(t,cos(t)./(1+t),'LineWidth',2)
plot(t,sin(t)./(1+t),'LineWidth',2)
plot(t,cos(t+pi/2)./(1+t+pi/2),'LineWidth',2)
plot(t,cos(t+pi)./(1+t+pi),'LineWidth',2)
legend

codeprinter

点击:查看->属性编辑器

进行一通编辑:

关闭窗口后就能自动在命令行窗口生成一系列代码:

删掉代码末尾的codeprinter,把命令行的代码加载最后,能获取之前全部修饰效果,美滋滋:

t=0.01:0.2:3*pi;
hold on
plot(t,cos(t)./(1+t),'LineWidth',2)
plot(t,sin(t)./(1+t),'LineWidth',2)
plot(t,cos(t+pi/2)./(1+t+pi/2),'LineWidth',2)
plot(t,cos(t+pi)./(1+t+pi),'LineWidth',2)
legend

fig=gcf;
fig.Children(1).FontName='Cambria';
fig.Children(1).FontSize=11;
fig.Children(1).LineWidth=2;
fig.Children(1).Location='southeast';
fig.Children(1).NumColumns=4;
fig.Children(1).Orientation='horizontal';
fig.Children(1).Position=[0.4103, 0.12433, 0.48022, 0.028419];
fig.Children(1).Title.FontName='Cambria';
fig.Children(1).Title.FontSize=11;
fig.Children(2).XColor=[1, 0, 0];
fig.Children(2).XTick=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
fig.Children(2).XTickLabel='0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10';
fig.Children(2).YColor=[1, 0, 0];
fig.Children(2).ZColor=[1, 0, 0];
fig.Children(2).LineWidth=2;
fig.Children(2).XGrid='on';
fig.Children(2).YGrid='on';
fig.Children(2).Box='on';
fig.Children(2).Title.Position=[5, 1.0061, -1.4211e-14];
fig.Children(2).Title.String='demo of codeprinter';
fig.Children(2).Title.FontName='Cambria';
fig.Children(2).Title.FontSize=16;
fig.Children(2).Subtitle.Position=[5, 1.0042, -1.4211e-14];
fig.Children(2).XLabel.Color=[1, 0, 0];
fig.Children(2).XLabel.Position=[5, -0.4549, -1];
fig.Children(2).YLabel.Color=[1, 0, 0];
fig.Children(2).YLabel.Position=[-0.58126, 0.3, -1];
fig.Children(2).ZLabel.Color=[1, 0, 0];
fig.Children(2).Children(4).LineWidth=4;
fig.Children(2).Children(4).Marker='<';
fig.Children(2).Children(4).MarkerSize=8;


工具函数完整代码

function codeprinter(fig)
% @author : slandarer
% 公众号  : slandarer随笔
% 知乎    : hikari

if nargin<1
    fig=gcf;
end

objCoeList='ContextMenu','Title','Subtitle','Toolbar',...
    'XLabel','YLabel','ZLabel';% 'XAxis','YAxis','ZAxis'
coeList='CurrentPoint','BeingDeleted','TightInset','NextSeriesIndex',...
    'Type','Parent','Children','ContextMenu','Toolbar','Extent','Title','Subtitle',...
    'XAxis','YAxis','ZAxis','XLabel','YLabel','ZLabel','Legend','Interactions',...
    'ButtonDownFcn','CreateFcn','DeleteFcn','ItemHitFcn','Selected','Layout',...
    'FontSizeMode','XTickMode','TickMode','ZTickMode','XTickLabelMode',...
    'YTickLabelMode','ZTickLabelMode','TickDirMode','XLimMode','YLimMode','ZLimMode',...
    'XLimitMethod','YLimitMethod','ZLimitMethod','XColorMode','YColorMode','ZColorMode',...
    'GridColorMode','GridAlphaMode','MinorGridColorMode','MinorGridAlphaMode',...
    'ALimMode','CLimMode','DataAspectRatioMode','PlotBoxAspectRatio','CameraPositionMode',...
    'CameraTargetMode','CameraUpVectorMode','CameraViewAngleMode','NumColumnsMode',...
    'PickableParts','PositionConstraint','MarkerMode','ContextMenuOpeningFcn',...
    'XData','YData','ZData','CData','VertexNormals','VertexNormalsMode',...
    'FaceNormals','FaceNormalsMode';
for i=1:length(fig.Children)
    FCStruct=get(fig.Children(i));
    fNames=fieldnames(FCStruct);
    for n=1:length(coeList)
        coePos=strcmp(fNames,coeListn);
        fNames(coePos)=[];
    end
    for n=1:length(fNames)
        oriFig.Children(i).(fNamesn)=fig.Children(i).(fNamesn);
    end
    oriFig.Children(i).FNames=fNames;

% -------------------------------------------------------------------------
    for j=1:length(fig.Children(i).Children)
        FCCStruct=get(fig.Children(i).Children(j));
        fNames=fieldnames(FCCStruct);
        for n=1:length(coeList)
            coePos=strcmp(fNames,coeListn);
            fNames(coePos)=[];
        end
        for n=1:length(fNames)
            oriFig.Children(i).Children(j).(fNamesn)=fig.Children(i).Children(j).(fNamesn);
        end
        oriFig.Children(i).Children(j).FNames=fNames;
    end
% -------------------------------------------------------------------------
    FCStruct=get(fig.Children(i));
    fsNames=fieldnames(FCStruct);
    for j=1:length(objCoeList) 
        if any(strcmpi(fsNames,objCoeListj))
            if ~isempty(fig.Children(i).(objCoeListj))
                FCOStruct=get(fig.Children(i).(objCoeListj));
                fNames=fieldnames(FCOStruct);
                for n=1:length(coeList)
                    coePos=strcmp(fNames,coeListn);
                    fNames(coePos)=[];
                end
                oriFig.Children(i).(objCoeListj)=[];
                for n=1:length(fNames)
                    oriFig.Children(i).(objCoeListj).(fNamesn)=fig.Children(i).(objCoeListj).(fNamesn);
                end
                oriFig.Children(i).(objCoeListj).FNames=fNames;
            else
                oriFig.Children(i).(objCoeListj)=[];
            end
        end
    end
end
% =========================================================================
set(fig,'CloseRequestFcn',@my_closereq)
% =========================================================================
function my_closereq(~,~)
    disp('fig=gcf;')
    for ri=1:length(oriFig.Children)
        for rn=1:length(oriFig.Children(ri).FNames)
            outprint(['fig.Children(',num2str(ri),').',oriFig.Children(ri).FNamesrn,'='],...
                oriFig.Children(ri).(oriFig.Children(ri).FNamesrn),fig.Children(ri).(oriFig.Children(ri).FNamesrn))
        end

        rFCStruct=get(fig.Children(ri));
        rfsNames=fieldnames(rFCStruct);
        for rj=1:length(objCoeList)
            if any(strcmp(rfsNames,objCoeListrj))
                if ~isempty(oriFig.Children(ri).(objCoeListrj))
                    for rn=1:length(

以上是关于MATLAB | 怎么让MATLAB自己生成代码?的主要内容,如果未能解决你的问题,请参考以下文章

matlab的GUI中,怎么修改参数

matlab如何生成verilog

请问,怎么用matlab生成一个随机对称的矩阵

怎样用MATLAB生成VERILOG HDL

怎么用matlab生成随机数,排序,取数排序前的位置?

matlab怎么生成灰度图像