首页/文章/ 详情

在dymola中采用脚本命令绘图

3月前浏览6198

image.png

过冷水诚挚邀请你加入Matlab仿真秀官方交流群进行Matlab学习、问题咨询、 Matlab相关资料下载,**:927550334

QQ图片20210424105303.png

冷水开始使用dymola进行模型工作分析,在早期遇到的问题就是不知道,软件中的Script与Commards该怎么使用。

在分析仿真结果时需要绘制变量曲线的图:

image.png

过冷水在实践中发现,当如果绘图工作量比较大的时候,在Variable  Browser选择变量的时候就比较麻烦。 可以选择用New Script命令框里的Generate Script 生成绘图脚本命令:

image.png

选择Generate Script后,会弹出选择框,选择Plot setup,点击OK即可生成脚本文件*.Mos

image.png

将脚本文件命名为huitu.mos:

image.png

代码:

removePlots(false);
Advanced.FilenameInLegend = true;
Advanced.FilesToKeep = 16;
createPlot(id=1, position={0, 0, 1140, 393}, y={"Ag_Cu.y[1]"}, range={0.0, 10000.0, -0.5, 4.0}, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Cu_dymola.mat", legends={"Ag_Cu.y[1]"}, colors={{28,108,200}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 393}, y={"Ag_Ge.y[3]"}, range={0.0, 10000.0, -0.5, 4.0}, erase=false, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Ge_dymola.mat", legends={"Ag_Ge.y[1]"}, colors={{238,46,47}}, thicknesses={0.5}, timeUnit="s");

在控制面板找到Execute Script,并双击运行,便可生成绘制Ag_Cu.y[1],Ag_Ge.y[1]的图像。

未标题-2.gif

过冷水在实践中还发现,如果想用脚本命令在一个窗口中绘制多幅曲线图。

image.png

使用Generate Script得到的脚本命令为:

removePlots(false);
Advanced.FilenameInLegend = true;
Advanced.FilesToKeep = 16;
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Cu.y[1]"}, range={0.0, 10000.0, -1.0, 4.0}, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Cu_dymola.mat", legends={"Ag_Cu.y[1]"}, subPlot=101, colors={{28,108,200}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Cu.y[2]"}, range={0.0, 10000.0, -5.0, 15.0}, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Cu_dymola.mat", legends={"Ag_Cu.y[2]"}, subPlot=102, colors={{28,108,200}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Cu.y[3]"}, range={0.0, 10000.0, -1.0, 4.0}, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Cu_dymola.mat", legends={"Ag_Cu.y[3]"}, subPlot=103, colors={{28,108,200}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Ge.y[1]"}, range={0.0, 10000.0, -1.0, 4.0}, erase=false, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Ge_dymola.mat", legends={"Ag_Ge.y[1]"}, subPlot=101, colors={{238,46,47}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Ge.y[2]"}, range={0.0, 10000.0, -5.0, 15.0}, erase=false, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Ge_dymola.mat", legends={"Ag_Ge.y[2]"}, subPlot=102, colors={{238,46,47}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Ge.y[3]"}, range={0.0, 10000.0, -1.0, 4.0}, erase=false, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Ge_dymola.mat", legends={"Ag_Ge.y[1]"}, subPlot=103, colors={{238,46,47}}, thicknesses={0.5}, timeUnit="s");

运行脚本命令:

image.png

可得图像,存在的问题是Variable  Browser存在多组Ag_Cu_dymola,Ag_Ge_dymola数据,显然这是没有必要的,分析原因发现是createPlot命令中含有:

filename="C:/Users/wayne/Desktop/Ag_Ge_dymola.mat"

其存在会导致每次执行createPlot命令时,都会导入读取一次*.mat文件,因此导致Variable  Browser中有多组数据,采取对应的方法为,可以减少filename的使用,采用默认数据,因为有两种数据,可以先绘制Ag_Cu_dymola.mat"对应的三幅图,第一副图使用

filename="C:/Users/wayne/Desktop/Ag_Ge_dymola.mat"

后面的命令取消filename,采用默认,则对应代码为:

createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Cu.y[1]"}, range={0.0, 10000.0, -1.0, 4.0}, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Cu_dymola.mat", legends={"Ag_Cu.y[1]"}, subPlot=101, colors={{28,108,200}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Cu.y[2]"}, range={0.0, 10000.0, -5.0, 15.0}, autoscale=false, grid=true, legends={"Ag_Cu.y[2]"}, subPlot=102, colors={{28,108,200}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Cu.y[3]"}, range={0.0, 10000.0, -1.0, 4.0}, autoscale=false, grid=true, legends={"Ag_Cu.y[3]"}, subPlot=103, colors={{28,108,200}}, thicknesses={0.5}, timeUnit="s");

运行代码得到的图像为:

image.png

图像如我们预期,Variable  Browser里面也只有一个Ag_Cu_dymola,则完整的代码和对应图像为:

image.png

removePlots(false);
Advanced.FilenameInLegend = true;
Advanced.FilesToKeep = 16;
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Cu.y[1]"}, range={0.0, 10000.0, -1.0, 4.0}, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Cu_dymola.mat", legends={"Ag_Cu.y[1]"}, subPlot=101, colors={{28,108,200}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Cu.y[2]"}, range={0.0, 10000.0, -5.0, 15.0}, autoscale=false, grid=true, legends={"Ag_Cu.y[2]"}, subPlot=102, colors={{28,108,200}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Cu.y[3]"}, range={0.0, 10000.0, -1.0, 4.0}, autoscale=false, grid=true, legends={"Ag_Cu.y[3]"}, subPlot=103, colors={{28,108,200}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Ge.y[1]"}, range={0.0, 10000.0, -1.0, 4.0}, erase=false, autoscale=false, grid=true, filename="C:/Users/wayne/Desktop/Ag_Ge_dymola.mat", legends={"Ag_Ge.y[1]"}, subPlot=101, colors={{238,46,47}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Ge.y[2]"}, range={0.0, 10000.0, -5.0, 15.0}, erase=false, autoscale=false, grid=true, legends={"Ag_Ge.y[2]"}, subPlot=102, colors={{238,46,47}}, thicknesses={0.5}, timeUnit="s");
createPlot(id=1, position={0, 0, 1140, 509}, y={"Ag_Ge.y[3]"}, range={0.0, 10000.0, -1.0, 4.0}, erase=false, autoscale=false, grid=true, legends={"Ag_Ge.y[1]"}, subPlot=103, colors={{238,46,47}}, thicknesses={0.5}, timeUnit="s");

这样我们就基本做到了使用脚本命令来实现绘图,本案例由于简单,我们是采用先绘图,在使用Generate Script得到对应的脚步命令,这样看起来似乎脚本命令没有实际用途,过冷水的目的也是为了演示如何获取脚本命令的典型案例代码,在后续工作中,就可以知道.mat文件后,直接使用脚本命令绘图即可,这样效率会非常高。

matlab绘制农夫过河动态图

分子动力学的原子空间运动轨迹演示编程

过冷水带你用matlab制作演示动画

python批量移动文件&重命名代码分享

过冷水和你分享 matlab读取存储各种文件的方法 文末有独家金曲分享


未标题-7.gif

附件

免费mat格式文件.zip
ToscaDYMOLA控制
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2024-02-09
最近编辑:3月前
过冷水
博士 | 讲师 讨论号:927550334
获赞 355粉丝 175文章 109课程 11
点赞
收藏
未登录
还没有评论

课程
培训
服务
行家

VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈