PDA

View Full Version : Writing new files from Matlab


raulinhoo
29 May 2008, 07:59pm
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!" http://www.kluid.com/mlib/images/smiles/icon_redface.gif

shwaip
31 May 2008, 09:00am
if you're opening a file for writing, rather than specifying 'r+' to fopen, you should specify 'w'

raulinhoo
2 Jun 2008, 11:25am
thanks! I have changed my scripts accordingly. will let u know if i need your help again.

regards,
raul

raulinhoo
3 Jun 2008, 01:16pm
hello,

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:

fid3=fopen('abaqus_data_aux3.txt','r+')

i=0

fid4= fopen('results.wri','w+')

data_line=fgetl(fid3)
while(fid3>=0)
if(i>2)
fprintf(fid4,'%s\n',data_line)
end

while ~feof(fid3)
i=i+1
data_line=fgetl(fid3)

if ~isempty(data_line), break
end
end


end

hope you can help me. Thanks!

Regards,

Raul

shwaip
4 Jun 2008, 03:02pm
Exactly what are you trying to do here?

raulinhoo
4 Jun 2008, 03:53pm
well, I have solved the problem to get out of the loop. but to answer your question, Im trying to to read the file "'abaqus_data_aux3.txt'" which has data I need to plot on a graph. The data is stored as strings, so i need to extract this data into numerics so that I can plot the graph. I am trying to sort this out now. my code looks like this now:

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

shwaip
5 Jun 2008, 08:31pm
what's the format of the file?

Consider using dlmread or csvread

(check the help if you don't know how).