Converting string scalar to written code

Is there a way to convert a string (suppose you have a variable str = "case 'newcase', x = 5; ") into written code in a script (not only to execute it) ?

 採用された回答

Walter Roberson
Walter Roberson 2023 年 12 月 12 日

1 投票

You can write text to a file using writelines() or by using fopen() / fprintf() / fclose(), or by using fopen() / fwrite() / fclose()
str = "case 'newcase', x = 5;"
str = "case 'newcase', x = 5;"
writelines(str, 'testscript.m');
dbtype testscript.m
1 case 'newcase', x = 5;
If you are wanting to write text into the middle of an existing file then unfortunately it gets a little more complicated.

1 件のコメント

Burçak
Burçak 2023 年 12 月 12 日
Yes, actually I needed to write in the middle but definitely that's the way! Thanks

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2023 年 12 月 11 日

コメント済み:

2023 年 12 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by