Creating string lines with Matlab Code
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Les Beckham
2022-4-15
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ß
2022-4-16
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!!!!
Walter Roberson
2022-4-18
编辑:Walter Roberson
2022-4-18
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ß
2022-4-18
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
2022-4-18
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ß
2022-4-18
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).
Walter Roberson
2022-4-18
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
2022-4-18
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ß
2022-4-18
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ß
2022-4-18
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')
回答(1 个)
Walter Roberson
2022-4-15
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 个评论
Christopher Schoß
2022-4-18
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"?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
