Dynamically change .txt file name in MATLAB, COMSOL simulation

10 views (last 30 days)
Marissa Whitby
Marissa Whitby on 7 Oct 2021
Commented: Marissa Whitby on 7 Oct 2021
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;

Accepted Answer

Jan
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.
  3 Comments
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!