MATLAB: Regexprep replacing two or more variables in a string of text (once) with ‘,’ between them

regexprepreplacing some variables to text string in on step

hi a have bellow code for change an string in my text file
fid = fopen('test.txt','r')
line = 0;
all ={};
while line ~= -1
line = fgets(fid);
if line ~= -1
all = [all;line];
end
end
fclose(fid) ;
buffer = fileread('test.txt') ;
i=4
data = (all{i})
c=300
d= 200
b = 'c , d'
buffer = regexprep(buffer, data,b) ;
fid = fopen('test.txt', 'w') ;
fwrite(fid, buffer) ;
fclose(fid) ;
i want to write c and d both with a ',' between them data (specific line) , but it (regexprep) not accept both of them with that format or i cant use it truly if anyone know how to fix that help! thanks

Best Answer

What is the purpose of the regexprep call? Is all you want to do to replace the 4.th line of the file by "300 , 200"? Then:
Data = strsplit(fileread('test.txt'), char(10)); % Or TEXTSCAN
Data{4} = sprintf('%g, %g', 300, 200);
fid = fopen('test.txt', 'w');
if fid == -1, error('Cannot open file for writing'); end
fprinft(fid, '%s\n', Data{:});
fclose(fid);