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

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!

 Accepted Answer

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

You're welcome!
(Consider accepting the answers that helped you.)
Yes you helped me. I have another unexcepted output:
output = "test_first_line";
output = output + newline + "test_second_line";
for i=1:3
output = output + newline + "model.param.set(nameX,(i,1)) '[mm]']);";
output = output + newline + "model.param.set(nameY,(i,2)) '[mm]']);";
end
outfilename = 'output.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
Output:
test_first_line
test_second_line
model.param.set(nameX,(i,1)) '[mm]']);
model.param.set(nameY,(i,2)) '[mm]']);
model.param.set(nameX,(i,1)) '[mm]']);
model.param.set(nameY,(i,2)) '[mm]']);
model.param.set(nameX,(i,1)) '[mm]']);
model.param.set(nameY,(i,2)) '[mm]']);
Why is my variable i not beeing inserted in the output
THANK YOU!
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

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2021a

Tags

Community Treasure Hunt

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

Start Hunting!