Creating Output.m file from Work.m file - unexpected output
2 views (last 30 days)
Show older comments
Christopher Schoß
on 19 Apr 2022
Commented: Christopher Schoß
on 20 Apr 2022
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!
0 Comments
Accepted Answer
Just Manuel
on 19 Apr 2022
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 Comments
Just Manuel
on 20 Apr 2022
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
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!