Writing new files from Matlab
Hello!
Im a new Matlab user and would like to appeal to anyone who might be able to help me with what im sure is a fairly simple problem.
I need to read a file (a python script file) into Matlab and then rewrite all the contents of this file to a new file (new python script file) with the exception of one line which will have to be changed with a new variable which will be generated in Matlab. I have written the code below so far but it doesn't do what I want.
<table align="center" border="0" cellpadding="3" cellspacing="1" width="90%"> <tbody><tr> <td>Code:</td> </tr> <tr> <td class="code">fid = fopen('abaqus_gear_file56.py','r+');
fid2 = fopen('abaqus_gear_file_final.py','r+');
EEE=2.5;
for ii=1:37
data_line=fgetl(fid)
fprintf(fid2,data_line,'\n')
end
loc = 3;
for i = 1:loc
temp_line = fgetl(fid);
end;
fprintf(fid,'\n');
fseek(fid,0,'cof');
fprintf(fid,'%g',EEE);
fprintf(fid,'\n');
fclose(fid); </td> </tr> </tbody></table>
this gives me the new file with all the lines read from the old file written onto one single line, except for the new line which is written onto a separate line. I hope I have explained my problem clearly, if it is not so, please do not hesitate to ask me. Thanks for all your help in advance! hope to hear back from you all soon. THANKS!!
Best regards,
Raul "stressed matlab user!"
Im a new Matlab user and would like to appeal to anyone who might be able to help me with what im sure is a fairly simple problem.
I need to read a file (a python script file) into Matlab and then rewrite all the contents of this file to a new file (new python script file) with the exception of one line which will have to be changed with a new variable which will be generated in Matlab. I have written the code below so far but it doesn't do what I want.
<table align="center" border="0" cellpadding="3" cellspacing="1" width="90%"> <tbody><tr> <td>Code:</td> </tr> <tr> <td class="code">fid = fopen('abaqus_gear_file56.py','r+');
fid2 = fopen('abaqus_gear_file_final.py','r+');
EEE=2.5;
for ii=1:37
data_line=fgetl(fid)
fprintf(fid2,data_line,'\n')
end
loc = 3;
for i = 1:loc
temp_line = fgetl(fid);
end;
fprintf(fid,'\n');
fseek(fid,0,'cof');
fprintf(fid,'%g',EEE);
fprintf(fid,'\n');
fclose(fid); </td> </tr> </tbody></table>
this gives me the new file with all the lines read from the old file written onto one single line, except for the new line which is written onto a separate line. I hope I have explained my problem clearly, if it is not so, please do not hesitate to ask me. Thanks for all your help in advance! hope to hear back from you all soon. THANKS!!
Best regards,
Raul "stressed matlab user!"
0
Comments
regards,
raul
I am stuck on another part of the same script I am writing. I have to find a way of terminating the loop. I am reading a file (abaqus_data_aux3) and need to read it until i reach the end of file and then terminate. I can't seem to do this and the matlab process keeps carrying on in the loop and i have to terminate the program. This is my code below: hope you can help me. Thanks!
Regards,
Raul
fid3=fopen('abaqus_data3.txt','r+')
i=0
kk=0;
fid4= fopen('results.wri','w+')
data_line=fgetl(fid3)
while(fid3>=0)&& kk==0
if(i>2)
end
while ~feof(fid3)
i=i+1
data_line=fgetl(fid3)
a=str2num(data_line)
if length(data_line)==0,
kk=1
end
fprintf(fid4,'%s\n',data_line)
end
end
I am stuck at the point where I want the variable "a" to store all the values, and not just the last one. any input from you is welcome. thanks!
Regards,
Raul
Consider using dlmread or csvread
(check the help if you don't know how).