Using m script in matlab

5 ビュー (過去 30 日間)
ANAND VISHAL
ANAND VISHAL 2020 年 2 月 26 日
コメント済み: ANAND VISHAL 2020 年 4 月 2 日
How can I move to a specific position in a text file and write a text there?
example:
volatile real32_T AutoWakeUpBalncDlyT_T_Pt = 3.0F;/* Delay time to distinguish to go to sleep. */
before
(AutoWakeUpBalncDlyT_T_Pt)
I have to add
Rom_
expected :
volatile real32_T Rom_AutoWakeUpBalncDlyT_T_Pt = 3.0F;/* Delay time to distinguish to go to sleep. */
  1 件のコメント
ANAND VISHAL
ANAND VISHAL 2020 年 2 月 26 日

AutoWakeUpBalncDlyT_T_Pt name is different in text file then? only First word volatile is constant in text file. I have to insert at third position after real32_T and real32_T may also change.

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

採用された回答

darova
darova 2020 年 2 月 26 日
What about strrep?
old = 'AutoWakeUpBalncDlyT_T_Pt';
new = 'Rom_AutoWakeUpBalncDlyT_T_Pt';
fid = fopen('test1.txt','r');
str1 = fscanf(fid,'%c');
fclose(fid);
str2 = strrep(str1,old,new);
fid = fopen('test2.txt','w');
fprintf(fid,'%c',str2);
fclose(fid);
  30 件のコメント
Walter Roberson
Walter Roberson 2020 年 4 月 2 日
This Question is tagged as being about R2017b. You talk about installing MinGW from R2018b (a different version). Your error message shows that you tried that in R2014b, a third version yet.
MinGW was not supported at the time of R2014b.
For R2014b, you need SDK 7.1 (but that can be difficult to install if you are using Windows 10), or you need a Profession (**not** Community or Express) version of Visual Studios.
ANAND VISHAL
ANAND VISHAL 2020 年 4 月 2 日
no for 2018b?

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 2 月 27 日
fopen the file for 'a+' permission, and without the t attribute. It is important that you do not use 't'
Now fseek() on the fid, specifying you want movement relative to beginning of file, and specifying exactly how many bytes to count from the beginning. For this purpose you must know whether the file is using CR+LF or only LF because those CR change the byte count. The count is not the number of visible characters, it is the number of bytes. There is no way to seek by "lines" in a variable length line file: you seek by bytes. (There is a technical exception that is pretty restrictive.)
Once you have used fseek() to move to a byte position in the file that you opened with 'a+', you can proceed to fwrite() to replace bytes in the file.
Notice this is strictly replacement. There is no way to insert bytes. Therefore the only way to insert the Rom_ prefix is to replace every byte to the end of file.
If at some point you want to stop replacing bytes and move elsewhere in the file you must fseek().
If you fread() or similar some bytes and you reach end of file, then before you can write any bytes past the end of file, you must fseek(). Even if the fseek() says to move 0 bytes relative to where you are, the fseek() must be done between reading and writing. The fseek is important for internal buffer management including proper handling of cache and of the end of file flag.
So.. That is how you do what you asked for. It will not do what you want.
To do what you want, read in the entire file as one character vector, and use routines such as strrep or regexprep to produce a modified version of it in memory, after which you fopen 'w' (not 'wt') and fwrite() the memory buffer to file.
  2 件のコメント
ANAND VISHAL
ANAND VISHAL 2020 年 2 月 27 日
it's not working. Can you snippet the code...
Walter Roberson
Walter Roberson 2020 年 2 月 27 日
in_filename = 'Whatever.c';
out_filename = ['Rom_', in_filename];
S = fileread(in_filename);
newS = regexprep(S, '(volatile\s+\w+\s+)', '$1Rom_');
fid = fopen(out_filename, 'w');
fwrite(fid, newS);
fclose(fid)

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

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by