Creating string lines with Matlab Code
Show older comments
Hello,
i want to create the following lines with my Matlab Code:
model.component('comp1').coordSystem.create('sys1', 'Cylindrical');
model.component('comp1').coordSystem.create('sys2', 'Cylindrical');
model.component('comp1').coordSystem.create('sys3', 'Cylindrical');
But its not 3 rows in every case so this number should be a variable.
I guess I need to work with a loop but I wont make it work.
Can someone help me?
THANK YOU!
12 Comments
Les Beckham
on 15 Apr 2022
Can you clarify what you mean by "create the following lines"? Do you want to print these lines to an output file? To the command window?
Christopher Schoß
on 16 Apr 2022
Christopher Schoß
on 18 Apr 2022
Walter Roberson
on 18 Apr 2022
Edited: Walter Roberson
on 18 Apr 2022
It is a valid software setup to have one set of code that uses configuration files to generate a second set of code, where the second set of code is going to be run repeatedly.
If you happen to have the setup where any one set of configuration files is going to result in creating .m code that will only be used once, then that kind of setup is not recommended. If you were using struct arrays I would suggest using setfield() https://www.mathworks.com/help/matlab/ref/setfield.html but I do not know if that would work with objects.
Christopher Schoß
on 18 Apr 2022
Walter Roberson
on 18 Apr 2022
component_prefix = "comp";
component_number = 1:2; %row
sys_prefix = "sys";
sys_numbers = (1:3).'; %column
output = "model.component('" + component_prefix + component_number + "').coordSystem.create('" + sys_prefix + sys_numbers + "', 'Cylindrical')";
outfilename = 'comsol_project_11b.m';
[fid, msg] = fopen(outfilename, 'wt');
if fid < 0; error('Failed to open output file because "%s"', msg); end
fprintf(fid, '%s\n', output(:));
fclose(fid)
clear(outfilename); %clears any cached script
after which you can run the output file
Christopher Schoß
on 18 Apr 2022
Christopher Schoß
on 18 Apr 2022
Walter Roberson
on 18 Apr 2022
What quotation marks in the output?
The output has lines such as
model.component('comp1').coordSystem.create('sys1', 'Cylindrical')
Are you indicating that you instead want
model.component(comp1).coordSystem.create(sys1, Cylindrical)
??
Walter Roberson
on 18 Apr 2022
You can build any combination of lines in advance and then write them all at once.
Or you could have different routines that append to the output file.
output = "model.component('" + component_prefix + component_number + "').coordSystem.create('" + sys_prefix + sys_numbers + "', 'Cylindrical')";
That line is creating a variable for later use in the fprintf() . You can use as many fprintf() as you need. There is likely no reason to return that variable from the code.... and if there does turn out to be a reason, then just concatenate the string arrays together.
so_far = [so_far; this_output(:)];
Christopher Schoß
on 18 Apr 2022
Christopher Schoß
on 18 Apr 2022
Answers (1)
component_prefix = "comp";
component_number = 1:2; %row
sys_prefix = "sys";
sys_numbers = (1:3).'; %column
output = "model.component('" + component_prefix + component_number + "').coordSystem.create('" + sys_prefix + sys_numbers + "', 'Cylindrical')"
Categories
Find more on Entering Commands 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!