フィルターのクリア

Repeat a string several times and put it somewhere in a text file

14 ビュー (過去 30 日間)
H R
H R 2017 年 3 月 29 日
回答済み: Jan 2017 年 3 月 29 日
1.a: I have a piece of code that generates a particular text 10 times (see below), how can I do this without a loop?
1.b. I have also a text file somewhere in which there is a particular symbol "[$1]" (excluding the quotation marks), I would like to replace [$1] with the resulting string in 1.a. ( If I use a loop as above to generate the string, it is easy, but I want to be able to do this operation without a loop)
s6=[];
for i=1:10
s1= 'this is a new house';
s2='Gas 1000 2 3 4 water ';
s2a=num2str(1000.900);
r=[s2a, char(10)];
s3=[s2,' ', r, char(10)];
s4='TIME';
s5=num2str([1:10]');
%s5=mat2str([1:100]');
rr='';
for j=1:size(s5,1)
rr=[rr,s5(j,:),char(10)];
end
s6= [s6 s1,char(10),s3,s4,char(10),rr];
end
I appreciate your comments and help.
  3 件のコメント
H R
H R 2017 年 3 月 29 日
searching is fine. My main problem is with 1a. Is there another method not to use a loop structure (i.e. for i=1:10) and use other method to combine the string 10 times and generate a new "single" string (not an array).
H R
H R 2017 年 3 月 29 日
I think I found a way to do it with repmat(s6, 1,10)

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

採用された回答

Jan
Jan 2017 年 3 月 29 日
For replacing the string:
Ins = repmat(s6, 1, 10);
Str = fileread(FileName);
Str = strrep(Str, '[$1]', Ins);
fid = fopen(FileName, 'w');
if fid == -1, error('Cannot open file %s', FileName); end
fwrite(fid, Str, 'char');
fclose(fid);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by