用Dymola将特定变量写入.txt或.mat

时间:2018-10-02 15:32:13

标签: string stream modelica dymola

我需要一种将模拟中的特定变量写入.txt或.mat的方法。

使用:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    CoinConverter kiosk = new CoinConverter();

    System.out.println("Welcome to ConversionKiosk by: xxx xxx.\nPlease deposit any number of coins and follow the number by the name of the coin.\nValid coin names are quarters, dimes, nickels, and pennies.\nPlease enter 'Cashout' to complete the transaction.");
    int x = 0;
    do{
        String coinCount = in.nextLine();
        if(coinCount.contains("quarters")) {
            coinCount = coinCount.replace(" quarters", "");

            kiosk.addQuarters(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(coinCount.contains("dimes")) {
            coinCount = coinCount.replace(" dimes", "");

            kiosk.addDimes(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(coinCount.contains("nickels")) {
            coinCount = coinCount.replace(" nickels", "");

            kiosk.addNickels(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");            
        }else if(coinCount.contains("pennies")) {
            coinCount = coinCount.replace(" pennies", "");

            kiosk.addPennies(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(coinCount.contains("quarter")) {
            coinCount = coinCount.replace(" quarter", "");

            kiosk.addQuarters(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(coinCount.contains("dime")) {
            coinCount = coinCount.replace(" dime", "");

            kiosk.addDimes(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(coinCount.contains("nickel")) {
            coinCount = coinCount.replace(" nickel", "");

            kiosk.addNickels(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");                
        }else if(coinCount.contains("penny")) {
            coinCount = coinCount.replace(" penny", "");

            kiosk.addPennies(Integer.parseInt(coinCount));
            coinCount = coinCount.replaceAll(coinCount, "");
        }else if(!coinCount.contains("quarter") || !coinCount.contains("quarters") || !coinCount.contains("dime") || !coinCount.contains("dimes") || !coinCount.contains("nickel") || !coinCount.contains("nickels") || !coinCount.contains("penny") || !coinCount.contains("pennies")) {
            System.out.println("Please enter a valid input.");
        }else {
            System.out.println("Please enter a valid input.");
        }
        String close = in.nextLine();
        if (close.contains("Cashout")) {
            System.out.println("Would you like another Transaction(y/n)?");
            String another = in.nextLine();
            if(another.contains("y")) {
                kiosk.getVoucher();
            }else {
                kiosk.getVoucher();
                x ++;
            }
        }
    }while (x == 0);
    in.close(); 
}
与在“模拟”选项卡上导出变量相比,

导致错误数据输出,没有一致性。 .txt文件中的值完全不同,没有可见的模式。

2 个答案:

答案 0 :(得分:3)

Modelica.Utilities.Streams.writeRealMatrix会做什么?

对我来说

Modelica.Utilities.Streams.writeRealMatrix(
  fileName="C:/temp/test.mat", 
  matrixName="testMatrix", 
  matrix=[1,2;4,5], 
  append=false, 
  format="7")

产生了预期的.mat文件。

为了能够在.mat文件中写入变量,您将必须将变量转换为(二维)矩阵-如果还不是一个。这可以通过使用方括号转换标量或向量来实现。这将为标量提供matrix=[1]或为向量提供matrix=[{1,2,3,4}]。使用matrix=1matrix={1,2,3,4}会引发错误。

答案 1 :(得分:2)

由于可以接受后期处理,因此有一些可用选项:

Dymola包含工具alist

alist -e var1 [-e var2 ...] inputFile outputFile

export data for var... as comma-separated values, suitable
for importing into Microsoft Excel.

Options: -a  write in ASCII format (default)
         -b  write in Matlab binary format

https://www.j-raedler.de/projects/DyMat/可用于读取结果文件并以您喜欢的任何格式(使用Python)生成文件。

OpenModelica具有类似于alist的选项,如果您想要更少的数据点https://openmodelica.org/doc/OpenModelicaUsersGuide/v1.12.0/scripting_api.html#filtersimulationresults,它也可以对信号进行重新采样。