matlab - how to replace a string(after reading file)and store the updated content in a new file? -


i new @ matlab scripting, trying read contents of file(data_files) array, c, compare array specific string('hello'), if string found, replace string('ciao') , update file accordingly , store in new file(newfile.txt). have following code, please me figure out error:

c = textread(data_files, '%s', 'delimiter', '\n'); file_content = fileread(data_files); expr ='\hello'; fileread_info = regexp(filetext, expr, 'match'); length_fileread_info=length(fileread_info);         if length_fileread_info >=1                                     c = c(cellfun(@isempty, strrep(filetext,'hello','ciao') ));         end fid = fopen('newfile.txt', 'wt');  fprintf(fid, '%s\n', c{:}); fclose(fid); end 

i think not implementing cellfun correctly. following error

??? error using ==> cellfun input #2 expected cell array, char instead. 

advice, please!

you can yse regexprep:

>> newc = regexprep( file_content, 'hello', 'ciao' ); >> fid = fopen('newfile.txt', 'wt');  >> fprintf(fid, '%s', newc); >> fclose(fid); 

Comments