MATLAB | 怎么让MATLAB自己生成代码?
2023-09-14 09:06:48 时间
国赛就是明天了,写了个很有意思的东西,虽然目前只是个雏形但是,这玩意真的挺有意思的,很多人写完基础代码绘图后不咋修饰就点开属性编辑器一顿点点点,但这样画出的图毕竟只有图没有相关代码,有没有啥办法能够把点点点的过程转化为代码,能把论文写的更长一点?于是就有了下面这个小工具,首先介绍用法,最后给出工具函数完整代码(初代懒得写注释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,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 使用点云创建数字地面模型DSM
- matlab 二进制形状描述子
- 基于主从博弈理论的共享储能与综合能源微网优化运行研究(Matlab代码实现)
- 电力系统机组组合优化调度(IEEE14节点、IEEE30节点、IEEE118节点)(Matlab代码实现)
- 微电网重构|基于群稀疏性的机会约束微电网重构(Matlab代码和Python代码实现)
- 基于A*算法自动引导车的路径规划(Matlab代码实现)
- 【优化调度】基于模拟退火优化算法求解船舶优化调度问题(Matlab代码实现)
- 【路径优化】基于A*算法的路径优化问题(Matlab代码实现)
- 基于MCMC的交通量逆建模(Matlab代码实现)
- 基于MATLAB的车牌识别系统设计(Matlab代码实现)
- 【状态估计】基于PMU的多回路配电系统状态估计(Matlab代码实现)
- 微电网与电动车蓄电池优化(Matlab代码实现)
- 【灵敏度】OFDM 系统在 AWGN 信道下对不同载波频率偏移 (CFO) 的 BER 灵敏度研究(Matlab代码实现)
- 【信号处理】扩展卡尔曼滤波EKF(Matlab代码实现)
- m基于GA遗传优化算法的陆基制导系统地面站布设策略matlab仿真,并输出GDOP值
- 一维CNN,二维CNN以及三维CNN的训练模型matlab仿真
- LabVIEW使用MathScript Node或MATLAB脚本时出现错误1046
- 【机组组合】两阶段、多周期随机机组组合(SUC)的问题(Matlab代码实现)
- 未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)
- 基于双目图像三维建模算法的测量目标物体体积计算matlab仿真