現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Creating string lines with Matlab Code
5 ビュー (過去 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 で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)