Using m script in matlab

7 visualizaciones (últimos 30 días)
ANAND VISHAL
ANAND VISHAL el 26 de Feb. de 2020
Comentada: ANAND VISHAL el 2 de Abr. de 2020
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 comentario
ANAND VISHAL
ANAND VISHAL el 26 de Feb. de 2020

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.

Iniciar sesión para comentar.

Respuesta aceptada

darova
darova el 26 de Feb. de 2020
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 comentarios
Walter Roberson
Walter Roberson el 2 de Abr. de 2020
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 el 2 de Abr. de 2020
no for 2018b?

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 27 de Feb. de 2020
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 comentarios
ANAND VISHAL
ANAND VISHAL el 27 de Feb. de 2020
it's not working. Can you snippet the code...
Walter Roberson
Walter Roberson el 27 de Feb. de 2020
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)

Iniciar sesión para comentar.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by