Problem with saving table in a method

Please login with a confirmed email address before reporting spam

Hi everyone, I'm currently working on creating small scripts using methods in the Application Builder to simplify the export of data in the Model Builder. I call the equivalent method in the Model Builder to get it running.

Right now, I'm trying to save a table using the following script and ran into two issues:

model.result().evaluationGroup().create("eg10", "EvaluationGroup"); model.result().evaluationGroup("eg10").set("data", "dset1"); model.result().evaluationGroup("eg10").label("Iron Losses"); model.result().evaluationGroup("eg10").create("int1", "IntSurface"); model.result().evaluationGroup("eg10").feature("int1").label("Surface Iron Losses"); model.result().evaluationGroup("eg10").feature("int1").set("intvolume", true); model.result().evaluationGroup("eg10").feature("int1").selection().named("sel15"); model.result().evaluationGroup("eg10").feature("int1").set("expr", new String[]{"rmm.Qh"}); model.result().evaluationGroup("eg10").feature("int1").set("descr", new String[]{"Volumetric loss density, electromagnetic"}); model.result().evaluationGroup("eg10").feature("int1").set("unit", new String[]{"W/m"}); model.result().evaluationGroup("eg10").feature("int1").setIndex("expr", "rmm.Qh*L", 0); model.result().evaluationGroup("eg10").feature("int1").setIndex("descr", "P_iron_MOD", 0); model.result().evaluationGroup("eg10").run();
//model.result().evaluationGroup("eg10").save("Z:\Simulationsergebnisse\Iron_losses.txt");
//model.result().evaluationGroup("eg10").save("C:\Users\kzt\Documents\Iron_losses.txt");
//model.result().evaluationGroup("eg10").save("C:\Temp\Iron_losses.txt");

1.) Everything works fine up to the last commented-out line. Unfortunately, I can’t save to any of the specified directories and I always get the following error:

"Cannot open file for writing. - Filename: Z:\Simulationsergebnisse\Iron_losses.txt (with all the other paths as well) - Table: Iron Losses"

However, I can manually save to the paths via mouse click.

2.) Is there an alternative to the following line?

"model.result().evaluationGroup("eg10").feature("int1").selection().named("sel15");"

I’d like to use the name of the selection instead of the index number 15. When I add or remove selections, the index changes, so "15" may no longer refer to the correct selection.

Best regards and many thanks in advance for your help!

Tobias Zeller


3 Replies Last Post 2025年4月29日 GMT-4 11:58
Gunnar Andersson COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 14 hours ago 2025年4月29日 GMT-4 02:39
  1. For security reasons, methods by default have limited access to the file system. You can change this in Preferences > Methods and Java Libraries > File system access.
  2. The argument to selection().named() is the tag of a selection. This is the tag that you used when creating the selection (or was it created for you at some point)? In the former case, you can store the selection a variable, let's call it SEL, and pass SEL.tag() as argument to selection().named().
1. For security reasons, methods by default have limited access to the file system. You can change this in Preferences > Methods and Java Libraries > File system access. 2. The argument to selection().named() is the tag of a selection. This is the tag that you used when creating the selection (or was it created for you at some point)? In the former case, you can store the selection a variable, let's call it SEL, and pass SEL.tag() as argument to selection().named().

Please login with a confirmed email address before reporting spam

Posted: 11 hours ago 2025年4月29日 GMT-4 05:07
  1. For security reasons, methods by default have limited access to the file system. You can change this in Preferences > Methods and Java Libraries > File system access.
  2. The argument to selection().named() is the tag of a selection. This is the tag that you used when creating the selection (or was it created for you at some point)? In the former case, you can store the selection a variable, let's call it SEL, and pass SEL.tag() as argument to selection().named().

Dear Mr. Andersson, First of all, thank you very much for your reply. Unfortunately, I still have one remaining question.

  1. I’m not entirely sure how to interpret your answer. Is the tag the same as the label?

In my case, I have nearly 20 different selections (see attachment), and “sel15,” which is the 15th selection, is the one I need. If the label is not the tag, is there a way to change or view the tag of a selection? Additionally, when I create a new explicit selection, I don’t see an option for assigning a "tag."

If I determine my tag name—for example, if the tag name is “test”—what would my code look like? Would it be something like this? "model.result().evaluationGroup("eg10").feature("int1").selection().test.tag();"

Best regards and thanks a lot Tobias Zeller

>1. For security reasons, methods by default have limited access to the file system. You can change this in Preferences > Methods and Java Libraries > File system access. >2. The argument to selection().named() is the tag of a selection. This is the tag that you used when creating the selection (or was it created for you at some point)? In the former case, you can store the selection a variable, let's call it SEL, and pass SEL.tag() as argument to selection().named(). Dear Mr. Andersson, First of all, thank you very much for your reply. Unfortunately, I still have one remaining question. 2. I’m not entirely sure how to interpret your answer. Is the tag the same as the label? In my case, I have nearly 20 different selections (see attachment), and “sel15,” which is the 15th selection, is the one I need. If the label is not the tag, is there a way to change or view the tag of a selection? Additionally, when I create a new explicit selection, I don’t see an option for assigning a "tag." If I determine my tag name—for example, if the tag name is “test”—what would my code look like? Would it be something like this? "model.result().evaluationGroup("eg10").feature("int1").selection().test.tag();" Best regards and thanks a lot Tobias Zeller


Gunnar Andersson COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 4 hours ago 2025年4月29日 GMT-4 11:58

Every feature in the model has a tag and a label. By default only the label is shown in the GUI, and it's only the label that you can change - you can think of the tag as a more permanent identification (which is why it's used in the API). It's possible to enable showing the labels in the Model Builder, see attached screenshot.

If you want to find the tag for a given label, then you can write method code that loops over the selections in the model, retrieve the label for each selection using the label() method, compare against the label that you want, and then retrieve its tag using the tag() method.

If the tag is "test", then you would use it as follows:

model.result().evaluationGroup("eg10").feature("int1").selection().named("test")

Every feature in the model has a tag and a label. By default only the label is shown in the GUI, and it's only the label that you can change - you can think of the tag as a more permanent identification (which is why it's used in the API). It's possible to enable showing the labels in the Model Builder, see attached screenshot. If you want to find the tag for a given label, then you can write method code that loops over the selections in the model, retrieve the label for each selection using the label() method, compare against the label that you want, and then retrieve its tag using the tag() method. If the tag is "test", then you would use it as follows: model.result().evaluationGroup("eg10").feature("int1").selection().named("test")

Reply

Please read the discussion forum rules before posting.

Please log in to post a reply.

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.