You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Creating string lines with Matlab Code
2 views (last 30 days)
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
Actually I am not 100% sure on how to proceed. I want to automate modelling in a software (COMSOL) and want to use a Matlab .m-file for this. So the .m-file is what generates me my COMSOL model (there is a Livelink available in Matlab). But I have different parameters in Matlab which lead to different code-lines in my m-file and therefore different models in COMSOL. So for example my quoted problem with a variable and different amount of lines. The result should end in a m-file. So did I need maybe two m-files?? One where I code my for-loops etc and one where this is writing to?? Not really sure whats the best way.
Would appreciate any help!!!!
Christopher Schoß
on 18 Apr 2022
Any idea?
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
Okay. So in the end I need my second.m with just the format of code which I can run my COMSOL Software (via LiveLink). To generate the second.m I use my first file(also a m-file??) which consideres my input variables (I put them into the workspace?!) and printes the second.m. Is that a possible way?
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
Jeahhh perfect! Thank you!
Next to this part there is much more to right into the output.m
So similar parts with just different variables/formulations and also just constant phrases.
Would you suggest to put this all into this one work.m file or is this not possible because there can be just one line of output=... ?
Otherwise I would try to output different outputX.m files and combine than at the end (would be unfortunatly an addtional step then).
Christopher Schoß
on 18 Apr 2022
And I need to get rid of the quotation marks in the output
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
I mixed my models up so there are no quotations marks, its fine, thank you!
I am not sure if I get it right with the fprintf(), so for example I want to print this 3 parts in the comsol_project_11b.m:
model.component('comp1').coordSystem.create('sys1', 'Cylindrical')
model.component('comp1').coordSystem.create('sys2', 'Cylindrical')
model.component('comp1').coordSystem.create('sys3', 'Cylindrical')
model.component('comp2').coordSystem.create('sys1', 'Cylindrical')
model.component('comp2').coordSystem.create('sys2', 'Cylindrical')
model.component('comp2').coordSystem.create('sys3', 'Cylindrical')
Code.placeholder.without.input.variables
model.component('test').coordSystem.create('systest1', 'Cylindrical')
model.component('test').coordSystem.create('systest2', 'Cylindrical')
model.component('test').coordSystem.create('systest3', 'Cylindrical')
model.component('test2').coordSystem.create('systest1', 'Cylindrical')
model.component('test2').coordSystem.create('systest2', 'Cylindrical')
model.component('test2').coordSystem.create('systest3', 'Cylindrical')
_______________________
First part is what I already got.
Second part is just simple lines(like text) without prefixes what I get from simply this:
output = "Code.placeholder.without.input.variables";
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
Third part is a varaiation of part one which I would get from:
component_prefix = "test";
component_number = 1:2; %row
sys_prefix = "systest";
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
Not really find a working way to right this three part in one output file (the comsol_project_11b.m)
THANK YOU WALTER!!
Christopher Schoß
on 18 Apr 2022
the following line seems to help me:
output = output + newline
But the result duplicates the first output testlines for some reason:
MY CODE:
output = "test_line1";
output = output + newline + "test_line2";
output = output + newline + "test_line3;";
component_prefix = "comp";
component_number = 1:2; %row
sys_prefix = "sys";
sys_numbers = (1:3).'; %column
output = output + newline + "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
PRINTS:
test_line1
test_line2
test_line3;
model.component('comp1').coordSystem.create('sys1', 'Cylindrical')
test_line1
test_line2
test_line3;
model.component('comp1').coordSystem.create('sys2', 'Cylindrical')
test_line1
test_line2
test_line3;
model.component('comp1').coordSystem.create('sys3', 'Cylindrical')
test_line1
test_line2
test_line3;
model.component('comp2').coordSystem.create('sys1', 'Cylindrical')
test_line1
test_line2
test_line3;
model.component('comp2').coordSystem.create('sys2', 'Cylindrical')
test_line1
test_line2
test_line3;
model.component('comp2').coordSystem.create('sys3', 'Cylindrical')
Answers (1)
Walter Roberson
on 15 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')"
output = 3×2 string array
"model.component('comp1').coordSystem.create('sys1', 'Cylindrical')" "model.component('comp2').coordSystem.create('sys1', 'Cylindrical')"
"model.component('comp1').coordSystem.create('sys2', 'Cylindrical')" "model.component('comp2').coordSystem.create('sys2', 'Cylindrical')"
"model.component('comp1').coordSystem.create('sys3', 'Cylindrical')" "model.component('comp2').coordSystem.create('sys3', 'Cylindrical')"
2 Comments
Christopher Schoß
on 16 Apr 2022
Thank you!
Christopher Schoß
on 18 Apr 2022
Is it possible to right just the last 3 lines into an other file? Like an other m-file? So just the code I need without the "output = 3×2 string array"?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)