Creating string lines with Matlab Code

1 次查看(过去 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 个评论
Christopher Schoß
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ß
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
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ß
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 CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by