Borrar filtros
Borrar filtros

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

4 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

Just Manuel
Just Manuel el 19 de Abr. de 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 comentarios
Just Manuel
Just Manuel el 20 de Abr. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by