Creating string lines with Matlab Code

조회 수: 3 (최근 30일)
Christopher Schoß
Christopher Schoß 2022년 4월 15일
댓글: Christopher Schoß 2022년 4월 18일
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월 16일
Thank you!
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