필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
Christopher Schoß
Christopher Schoß 2022년 4월 19일
댓글: Christopher Schoß 2022년 4월 20일
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