How can I modifya string into a ".m" file from a new ".m" file?
Mostra commenti meno recenti
Hi. I'd like to know if is it possible to modify a string (or part of it) from outside, i.e. create a new ".m" file and modify, or change, strings of an existent ".m" file from the new one. Thanking you for your attention, I'm looking forward for your answers.
3 Commenti
Star Strider
il 10 Ott 2012
A .m file is simply a text file, so theoretically, yes.
Matt Kindig
il 10 Ott 2012
Hi Antonio,
Your question is a bit unclear. Are you saying that you want to do a search/replace in the m-file, and then write out the replaced file as a new file? If so, you can either use the "Find & Replace" utility in the Matlab IDE (Ctrl+F), or you can do it in code using something like this:
str = fileread('/path/to/your/mfile.m');
newstr = strrep(str, 'oldtext1', 'replacetext1');
newstr = strrep(newstr, 'oldtext2', 'replacetext2');
% etc.
%now write new file
fid = fopen('/path/to/newfile.m', 'wt');
fprintf(fid, '%s', newstr);
fclose(fid);
Antonio
il 11 Ott 2012
Risposte (1)
Image Analyst
il 10 Ott 2012
0 voti
Yes, use fopen, fgets or fgetl, fprintf, and fclose.
2 Commenti
Antonio
il 11 Ott 2012
Image Analyst
il 11 Ott 2012
Try strrep() instead of STRREP(). MATLAB is case sensitive.
Categorie
Scopri di più su Characters and Strings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!