Dynamically change .txt file name in MATLAB, COMSOL simulation
12 views (last 30 days)
Show older comments
I have exported code for my COMSOL simulation to MATLAB and I am working on looping through different parameter values for each run of the simulation. At the end of each simulation I need the data to be exported to a file (ideally .csv but it appears I can only do .txt). I need the file name to be updated each time the loop runs.
The problem with the code below is that fname is not recognized as a variable and the file name comes out to be
/Users/marissawhitby/Desktop/C:\Users\jmw\Desktop\Output_Texts\fname
I'm really confused how to do this using COMSOL specific code because I've done similar things in normal MATLAB code in the past.
Any suggestions on how to fix this naming of the .txt file or even how to also get it as a .csv is greatly appreciated.
Note: I've also tried just fname instead of the full file path but that doesn't seem to work either.
Code:
fname = sprintf('Results_E_Field_Iter%d.txt', iter);
model.result.export('data1').set('filename', '/Users/marissawhitby/Desktop/C:\Users\jmw\Desktop\Output_Texts\fname');
model.result.export('data1').set('header', false);
model.result.export('data1').run;
1 Comment
Rebeka Sultana
on 26 Feb 2022
Hello Marissa,
Is that export command is applicable for each loop iteration? I am working on a problem where the output result is posprocessing of data set (some matrix table) from a nested loop. But I am unable to save/export the table result for each iteration in an allocated path I want. Only the first result is being saved with "model.result.table('tbl1').save('Mutual.csv');" comand. Can you help me find out what I am missing here?
Accepted Answer
Jan
on 7 Oct 2021
Edited: Jan
on 7 Oct 2021
fname = sprintf('Results_E_Field_Iter%d.csv', iter);
file = fullfile('/Users/marissawhitby/Desktop/C:\Users\jmw\Desktop\Output_Texts', fname)
model.result.export('data1').set('filename', file);
Because fname is a variable, using it inside a char vector does not work. So simply use it as a variable.
Are you sure that the file path is correct? It looks neither as Unix nor as Windows.
5 Comments
ferdaous TRIBAK
on 7 Sep 2022
Hi Jad;
I exported the comsol simulation code exactly as it is. without any modification. (after saving the comsol simulation in .m format, I opened the file in matlab and run the code without any modification so i can simulated it in matlab and then work on optimization side. ; I got the following errors. what should I do to make it work? what does it mean
can you please help me?
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Error using Ferdaous (line 4630)
Java exception occurred:
Exception:
com.comsol.util.exceptions.FlException: Syntax error in expression
Messages:
Failed to evaluate expression for parameter Fzz.
Syntax error in expression.
- Expression: atan( (1/sqrt(((RT+H)/RT))*((RT+H)/RT))-1))-sqrt(((RT+H)/RT))*((RT+H)/RT))-1) )/pi
- Subexpression: )-sqrt(((RT+H)/RT))* ...
- Position: 43
Stack trace:
at com.comsol.util.classes.exprparser.ExprParser.a(SourceFile:75)
at com.comsol.util.classes.exprparser.ExprParser.a(SourceFile:214)
at com.comsol.util.classes.exprparser.ExprParser.a(SourceFile:126)
at com.comsol.util.classes.exprparser.ExprParser.a(SourceFile:273)
at com.comsol.util.classes.exprparser.ExprParser.a(SourceFile:253)
at com.comsol.util.classes.exprparser.ExprParser.a(SourceFile:241)
at com.comsol.nativeutil.parser.ParseUtil.a(SourceFile:1095)
at com.comsol.nativeutil.parser.ParseUtil.a(SourceFile:1066)
at com.comsol.applapi.variables.am.replaceWithCompleteNames(SourceFile:3499)
at com.comsol.model.applapi.UnitConverter.getUnitExpr(SourceFile:647)
at com.comsol.core.parameter.ParamDatabase.a(SourceFile:967)
at com.comsol.core.parameter.ParamDatabase.set(SourceFile:907)
at com.comsol.core.parameter.ParamDatabase.a(SourceFile:867)
at com.comsol.model.dbmodel.ModelParamDb.addParametersToDatabase(SourceFile:235)
at com.comsol.model.method.ModelMethod.updateParamDatabase(SourceFile:3079)
at com.comsol.core.parameter.ParamDatabase.b(SourceFile:141)
at com.comsol.core.parameter.ParamDatabase.a(SourceFile:116)
at com.comsol.model.method.ParamBaseMethod.updateDependentParamDatabases(SourceFile:340)
at com.comsol.model.method.ModelParamMethod.updateDependentParamDatabases(SourceFile:172)
at com.comsol.model.dbmodel.ParamBaseDb.a(SourceFile:165)
at com.comsol.model.dbmodel.ModelEntityDb.handleEvent(SourceFile:958)
at com.comsol.model.data.EventManager.a(SourceFile:650)
at com.comsol.model.data.EventManager.sendEvent(SourceFile:633)
at com.comsol.model.data.EventManager.a(SourceFile:625)
at com.comsol.model.data.EventManager.a(SourceFile:487)
at com.comsol.model.data.MList.a(SourceFile:122)
at com.comsol.model.data.MList.set(SourceFile:432)
at com.comsol.model.dbmodel.ModelParamDb.a(SourceFile:121)
at com.comsol.model.dbmodel.ModelParamDb.b(SourceFile:1)
at com.comsol.model.dbmodel.ParamBaseDb.a(SourceFile:551)
at com.comsol.model.dbmodel.ParamBaseDb.set(SourceFile:515)
at jdk.internal.reflect.GeneratedMethodAccessor122.invoke(Unknown Source)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.comsol.bridge.command.c.a(SourceFile:147)
at com.comsol.bridge.command.c.run(SourceFile:90)
at com.comsol.bridge.command.l.c(SourceFile:213)
at com.comsol.bridge.command.l.a(SourceFile:203)
at com.comsol.bridge.command.l$1.run(SourceFile:94)
at com.comsol.util.thread.SuspendableTasks$1.run(SourceFile:111)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Jan
on 7 Sep 2022
atan((1/sqrt(((RT+H)/RT))*((RT+H)/RT))-1))-sqrt(((RT+H)/RT))*((RT+H)/RT))-1))/pi
% ^
This closing parenthesis is orphaned. The last 3 closing parentheses seems to have no opening counterpart also.
But what is the connection to this thread? Your problem does not concern the question. So please open a new thread.
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!