Writing new files from Matlab

edited June 2008 in Science & Tech
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!" icon_redface.gif

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited May 2008
    if you're opening a file for writing, rather than specifying 'r+' to fopen, you should specify 'w'
  • edited June 2008
    thanks! I have changed my scripts accordingly. will let u know if i need your help again.

    regards,
    raul
  • edited June 2008
    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
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited June 2008
    Exactly what are you trying to do here?
  • edited June 2008
    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
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited June 2008
    what's the format of the file?

    Consider using dlmread or csvread

    (check the help if you don't know how).
Sign In or Register to comment.