Change the content in an m-file based on check-box input from gui

1 回表示 (過去 30 日間)
Luffy
Luffy 2015 年 10 月 13 日
コメント済み: Walter Roberson 2015 年 10 月 14 日
Hi,
I have a checkbox in my gui based on which i want to make some changes in another m-file & later run it.
The format of other m-file is like this:-
tclstart = {...
'# ...there is further code above.. #',...
'# .......................... #',...
'set compile 1',...
'# .......................... #',...
'# there is further code below'
}
Based on check-box value
get(h.checkbox1, 'Value');
if it is 1,i want set compile to be 1 in that m-file otherwise 0 as an m-file is similar to txt file,i'm doing this-
content = fileread('tcl.m'); % name of that other m-file
index = regexp(content,'set compile')
Here i'm getting starting index of the matched sub-string,now how can i change the value from 1 to 0/vice-versa.

採用された回答

Walter Roberson
Walter Roberson 2015 年 10 月 13 日
newvalue = char('0' + get(h.checkbox1, 'Value'));
newcontent = regexprep(content, '(?<=set compile\s+)[01]', newvalue);
  4 件のコメント
Luffy
Luffy 2015 年 10 月 13 日
編集済み: Luffy 2015 年 10 月 13 日
Yeah,didn't see this ?<=. But anyway trying that above snippet doesn't change the value in the m file.
To do a quick test,i did this:
content = fileread('tcl.m'); % name of that other m-file
newvalue = '0';
newcontent = regexprep(content, '(?<=set compile\s+)[01]', newvalue);
but the value in the m file remains unchanged
Walter Roberson
Walter Roberson 2015 年 10 月 14 日
The code snippet does not change the .m file: it returns the variable newcontent with the updated information. You can then write newcontent to whatever file.
fid = fopen('tcl.m', 'w');
fwrite(fid, newcontent);
fclose(fid);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by