필터 지우기
필터 지우기

How can I modifya string into a ".m" file from a new ".m" file?

조회 수: 9 (최근 30일)
Antonio
Antonio 2012년 10월 10일
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
Matt Kindig
Matt Kindig 2012년 10월 10일
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
Antonio 2012년 10월 11일
clc clear all
INTERV_DOWN=3000; INTERV_UP=4000;
TEXT = FILEREAD('changestrings.m');
MODIFIEDSTR = STRREP(TEXT,'1000','INTERV_DOWN') MODIFIEDSTR = STRREP(TEXT,'1500','INTERV_UP')
ERROR: ??? Undefined function or method 'STRREP' for input arguments of type 'char'.
Error in ==> aprire at 14 MODIFIEDSTR = STRREP(TEXT,'1000','INTERV_DOW')

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2012년 10월 10일
Yes, use fopen, fgets or fgetl, fprintf, and fclose.
  댓글 수: 2
Antonio
Antonio 2012년 10월 11일
clc clear all
INTERV_DOWN=3000; INTERV_UP=4000;
TEXT = FILEREAD('changestrings.m');
MODIFIEDSTR = STRREP(TEXT,'1000','INTERV_DOWN') MODIFIEDSTR = STRREP(TEXT,'1500','INTERV_UP')
ERROR: ??? Undefined function or method 'STRREP' for input arguments of type 'char'.
Error in ==> aprire at 14 MODIFIEDSTR = STRREP(TEXT,'1000','INTERV_DOW')
Image Analyst
Image Analyst 2012년 10월 11일
Try strrep() instead of STRREP(). MATLAB is case sensitive.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by