在MAT文件中导出Modelica变量,无需后期处理
目的是: 在MAT文件中输出一些特定变量,而不使用标准Modelica输出提供的复杂结构; 无需任何后处理,即在模拟后,我希望MAT文件准备就绪,无需进一步的步骤; 不使用特定于IDE的Dymola或OpenModelica命令; 可能不产生事件; 起初我想到的是Modelica.Utilities.Streams.writeRealMatrix实用程序,但我无法使append参数正常工作 以下是我尝试过的:在MAT文件中导出Modelica变量,无需后期处理,modelica,Modelica,目的是: 在MAT文件中输出一些特定变量,而不使用标准Modelica输出提供的复杂结构; 无需任何后处理,即在模拟后,我希望MAT文件准备就绪,无需进一步的步骤; 不使用特定于IDE的Dymola或OpenModelica命令; 可能不产生事件; 起初我想到的是Modelica.Utilities.Streams.writeRealMatrix实用程序,但我无法使append参数正常工作 以下是我尝试过的: model WriteRealMatrixToFileTest discrete
model WriteRealMatrixToFileTest
discrete Boolean success;
discrete Real time_pre(start=0, fixed=true);
equation
when sample(0,0.1) then
success = Modelica.Utilities.Streams.writeRealMatrix("OutMat.mat", "OutMat", {{time, sin(time)}}, append=true, format="4");
Modelica.Utilities.Streams.print("Printing status at time " + String(time) + ": " + String(success));
time_pre = time;
end when;
end WriteRealMatrixToFileTest;
但是,即使它总是成功返回,即使命令在每0.1s正确调用一次,在MAT文件中,我只找到最后一个值,例如{10,-0.544},如果stopTime=10
一种选择是读回MAT文件,将新的值集附加到矩阵中,然后重新写回,但这听起来真的很难听。还有其他方法吗?
因为我不能声明动态大小矩阵,所以我想不出其他选项。append参数可以工作,但它的行为与您期望的不一样。writeRealMatrix将变量附加到给定的.mat文件中,但不会将值附加到该文件中的现有矩阵中。如果存在具有此名称的变量,则会覆盖该变量 我们的想法是: 通过Modelica.Utilities.Streams.print将结果连续写入.txt文件,该文件会自动附加到现有文件中 在模拟结束时,读取.txt内容,并将其与writeRealMatrix一起作为.mat文件存储在when终端部分中 也许是这样:
model WriteRealMatrixToFileTest
import Modelica.Utilities.Streams;
import Modelica.Utilities.Strings;
Boolean success(start=false);
parameter String tmpFile = "OutMat.txt";
parameter String outFile = "OutMat.mat";
protected
function txt2mat "Convert txt file to mat file"
input String inFile;
input String outFile;
output Boolean success;
protected
String[:] lines;
Integer nlines;
Real t[:], v[:];
Integer pos "Start position of value at current line";
algorithm
lines :=Streams.readFile(inFile);
nlines :=size(lines, 1);
t :=fill(0, nlines); // we have to initialize t and v with the correct size
v :=fill(0, nlines); // so we can later use t[i] and v[i]
for i in 1:nlines loop
t[i] :=Strings.scanReal(lines[i], 1);
pos :=Strings.find(lines[i], ";")+1; // get delimiter position to scan v
v[i] :=Strings.scanReal(lines[i], pos);
end for;
success :=Streams.writeRealMatrix(outFile,"OutMat",{t,v},format="4");
end txt2mat;
equation
when initial() then
Modelica.Utilities.Files.removeFile(tmpFile);
end when;
when sample(0,0.1) then
Streams.print(String(time)+";"+String(sin(time)), fileName=tmpFile);
end when;
when terminal() then
success = txt2mat(tmpFile, outFile);
end when;
end WriteRealMatrixToFileTest;
append参数可以工作,但它的行为与您预期的不同。writeRealMatrix将变量附加到给定的.mat文件中,但不会将值附加到该文件中的现有矩阵中。如果存在具有此名称的变量,则会覆盖该变量 我们的想法是: 通过Modelica.Utilities.Streams.print将结果连续写入.txt文件,该文件会自动附加到现有文件中 在模拟结束时,读取.txt内容,并将其与writeRealMatrix一起作为.mat文件存储在when终端部分中 也许是这样:
model WriteRealMatrixToFileTest
import Modelica.Utilities.Streams;
import Modelica.Utilities.Strings;
Boolean success(start=false);
parameter String tmpFile = "OutMat.txt";
parameter String outFile = "OutMat.mat";
protected
function txt2mat "Convert txt file to mat file"
input String inFile;
input String outFile;
output Boolean success;
protected
String[:] lines;
Integer nlines;
Real t[:], v[:];
Integer pos "Start position of value at current line";
algorithm
lines :=Streams.readFile(inFile);
nlines :=size(lines, 1);
t :=fill(0, nlines); // we have to initialize t and v with the correct size
v :=fill(0, nlines); // so we can later use t[i] and v[i]
for i in 1:nlines loop
t[i] :=Strings.scanReal(lines[i], 1);
pos :=Strings.find(lines[i], ";")+1; // get delimiter position to scan v
v[i] :=Strings.scanReal(lines[i], pos);
end for;
success :=Streams.writeRealMatrix(outFile,"OutMat",{t,v},format="4");
end txt2mat;
equation
when initial() then
Modelica.Utilities.Files.removeFile(tmpFile);
end when;
when sample(0,0.1) then
Streams.print(String(time)+";"+String(sin(time)), fileName=tmpFile);
end when;
when terminal() then
success = txt2mat(tmpFile, outFile);
end when;
end WriteRealMatrixToFileTest;