Is there a command that writes code in another program?

1 回表示 (過去 30 日間)
Thomas Nicklaus
Thomas Nicklaus 2020 年 4 月 2 日
コメント済み: Thomas Nicklaus 2020 年 4 月 2 日
I need to have a program that writes another program based on user input.
All I need is a command thats writes code, like:
writecode(code,program_name,line);
example:
writecode('disp(x+variable^2)','program.m',24);
I have not been able to find any command to help me do this and I am not sure if one exists.

採用された回答

Image Analyst
Image Analyst 2020 年 4 月 2 日
fprintf() will do that.
fid = fopen('program.m', 'wt'); % Open the file for writing.
fprintf(fid, 'disp(x+variable^2)'); % Write your line of code.
fclose(fid); % Close the file.
  4 件のコメント
Steven Lord
Steven Lord 2020 年 4 月 2 日
Make sure that you write a newline to the file, either by calling fprintf to print what's returned by newline or by including \n in your format specifier:
fprintf('abc%cdef\nghi', newline)
The prompt will be printed right next to the i on the last line, since we didn't end that with a \n. I printed to the Command Window since I didn't specify a file identifier, but it works the same writing to a text file.
Thomas Nicklaus
Thomas Nicklaus 2020 年 4 月 2 日
Ok, thanks that works.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by