国赛就是明天了,写了个很有意思的东西,虽然目前只是个雏形但是,这玩意真的挺有意思的,很多人写完基础代码绘图后不咋修饰就点开属性编辑器一顿点点点,但这样画出的图毕竟只有图没有相关代码,有没有啥办法能够把点点点的过程转化为代码,能把论文写的更长一点?于是就有了下面这个小工具,首先介绍用法,最后给出工具函数完整代码(初代懒得写注释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
,把命令行的代码加载最后,能获取之前全部修饰效果,美滋滋:文章来源:https://www.toymoban.com/news/detail-540111.html
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;
文章来源地址https://www.toymoban.com/news/detail-540111.html
工具函数完整代码
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,coeList{n});
fNames(coePos)=[];
end
for n=1:length(fNames)
oriFig.Children(i).(fNames{n})=fig.Children(i).(fNames{n});
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,coeList{n});
fNames(coePos)=[];
end
for n=1:length(fNames)
oriFig.Children(i).Children(j).(fNames{n})=fig.Children(i).Children(j).(fNames{n});
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,objCoeList{j}))
if ~isempty(fig.Children(i).(objCoeList{j}))
FCOStruct=get(fig.Children(i).(objCoeList{j}));
fNames=fieldnames(FCOStruct);
for n=1:length(coeList)
coePos=strcmp(fNames,coeList{n});
fNames(coePos)=[];
end
oriFig.Children(i).(objCoeList{j})=[];
for n=1:length(fNames)
oriFig.Children(i).(objCoeList{j}).(fNames{n})=fig.Children(i).(objCoeList{j}).(fNames{n});
end
oriFig.Children(i).(objCoeList{j}).FNames=fNames;
else
oriFig.Children(i).(objCoeList{j})=[];
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).FNames{rn},'='],...
oriFig.Children(ri).(oriFig.Children(ri).FNames{rn}),fig.Children(ri).(oriFig.Children(ri).FNames{rn}))
end
rFCStruct=get(fig.Children(ri));
rfsNames=fieldnames(rFCStruct);
for rj=1:length(objCoeList)
if any(strcmp(rfsNames,objCoeList{rj}))
if ~isempty(oriFig.Children(ri).(objCoeList{rj}))
for rn=1:length(oriFig.Children(ri).(objCoeList{rj}).FNames)
outprint(['fig.Children(',num2str(ri),').',objCoeList{rj},'.',oriFig.Children(ri).(objCoeList{rj}).FNames{rn},'='],...
oriFig.Children(ri).(objCoeList{rj}).(oriFig.Children(ri).(objCoeList{rj}).FNames{rn}),...
fig.Children(ri).(objCoeList{rj}).(oriFig.Children(ri).(objCoeList{rj}).FNames{rn}))
end
else
end
end
end
for rj=1:1:length(fig.Children(ri).Children)
for rn=1:length(oriFig.Children(ri).Children(rj).FNames)
outprint(['fig.Children(',num2str(ri),').Children(',num2str(rj),').',oriFig.Children(ri).Children(rj).FNames{rn},'='],...
oriFig.Children(ri).Children(rj).(oriFig.Children(ri).Children(rj).FNames{rn}),...
fig.Children(ri).Children(rj).(oriFig.Children(ri).Children(rj).FNames{rn}))
end
end
end
delete(fig)
fprintf('\n')
end
% =========================================================================
function outprint(baseStr,obj1,obj2)
changed=false;
if ~isa(obj2,'cell')
if isa(obj2,'table')
else
if length(obj1)~=length(obj2)
changed=true;
elseif ~all(all(all(obj1==obj2)))
changed=true;
end
end
else
if length(obj1)~=length(obj2)
changed=true;
elseif ~all(all(all(strcmp(obj1,obj2))))
changed=true;
end
end
if changed
fprintf('%s',baseStr)
switch true
case isa(obj2,'double')
tdouble=obj2;
if size(tdouble,1)==0
fprintf('%s\n','[];');
elseif numel(tdouble)==1
fprintf('%s\n',[slnum2str(tdouble),';']);
elseif size(tdouble,1)==1
fprintf('%s\n',['[',slnum2str(tdouble),'];']);
else
fprintf('%s','[')
for k=1:size(tdouble,1)-1
fprintf('%s\n',[slnum2str(tdouble(k,:)),';']);
end
fprintf('%s\n',[slnum2str(tdouble(size(tdouble,1),:)),'];']);
end
case isa(obj2,'cell')
fprintf('%s','{');
for k=1:length(obj2)-1
fprintf('%s',["'",obj2{k},"', "]);
end
fprintf('%s',["'",obj2{length(obj2)},"'};"]);
fprintf('\n');
case isa(obj2,'matlab.lang.OnOffSwitchState')||isa(obj2,'char')
fprintf('%s',["'",obj2,"';"]);
fprintf('\n');
end
end
end
% =========================================================================
function str=slnum2str(num)
tstr=num2str(num);
tstr=split(tstr);
str=tstr{1};
for k=2:length(tstr)
str=[str,', ',tstr{k}];
end
end
end
到了这里,关于MATLAB | 怎么让MATLAB自己生成代码?的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!