Creating Output.m file from Work.m file - unexpected output

4 次查看(过去 30 天)
Hey,
I just want to create an Output in form of an m-file from an existing file Work.m.
Unfortunatly I get some unexpected result.
My Code of the Work.m:
%CODE FOR OUTPUT-FILE PART 1
output = "test_line1";
output = output + newline + "test_line2";
%CODE FOR OUTPUT-FILE PART 2
component_prefix = "test";
component_number = 1:2; %row
sys_prefix = "sys";
sys_numbers = (1:2).'; %column
output = output + newline + "model('" + component_prefix + component_number + "').system('" + sys_prefix + sys_numbers + "', 'test')";
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
Resulting Output.m:
test_line1
test_line2
model('test1').system('sys1', 'test')
test_line1
test_line2
model('test1').system('sys2', 'test')
test_line1
test_line2
model('test2').system('sys1', 'test')
test_line1
test_line2
model('test2').system('sys2', 'test')
What I was trying to get:
test_line1
test_line2
model('test1').system('sys1', 'test')
model('test1').system('sys2', 'test')
model('test2').system('sys1', 'test')
model('test2').system('sys2', 'test')
I would appreciate any help!!
Thank you!

采纳的回答

Just Manuel
Just Manuel 2022-4-19
The part
"model('" + component_prefix + component_number + "').system('" + sys_prefix + sys_numbers + "', 'test')";
creates a 2x2 string matrix, where you add your previous output string to.
concatenate those strings first:
s = newline + "model('" + component_prefix + component_number + "').system('" + sys_prefix + sys_numbers + "', 'test')";
output = output + join(join(s, ''),'');
  5 个评论
Just Manuel
Just Manuel 2022-4-20
By accepting, I mean hitting that little Button "Accept this Answer" at the top of this answer. That flags this question thread as answered.
Your variable i is not inserted, because you only add a string to your output. You can use the same technique you used in your original question to insert the value of i in your string.
Cheers
Manuel

请先登录,再进行评论。

更多回答(0 个)

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by