Dynamically change .txt file name in MATLAB, COMSOL simulation

23 views (last 30 days)
Marissa Whitby
Marissa Whitby on 7 Oct 2021
Commented: Rebeka Sultana on 26 Feb 2022
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
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.
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);
  1 Comment
Rebeka Sultana
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?

Sign in to comment.

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.
Marissa Whitby
Marissa Whitby on 7 Oct 2021
Nevermind I figured it out thank you very much for your help!! That was very helpful I really appreciate it.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!