To talk on Icrontic, just register!

It only takes 30 seconds.

Have an account? Sign in:

Forgot?
raulinhoo
Getting settled in
raulinhoo
6 Posts

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.

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);


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!"
shwaip
elaborate bot
shwaip
5,729 Posts
if you're opening a file for writing, rather than specifying 'r+' to fopen, you should specify 'w'
__________________ my photostream for ic photography challenge

Anyone who wants dropbox, please use my referral link
raulinhoo
Getting settled in
raulinhoo
6 Posts
thanks! I have changed my scripts accordingly. will let u know if i need your help again.

regards,
raul
raulinhoo
Getting settled in
raulinhoo
6 Posts
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:
Code:
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
elaborate bot
shwaip
5,729 Posts
Exactly what are you trying to do here?
raulinhoo
Getting settled in
raulinhoo
6 Posts
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
elaborate bot
shwaip
5,729 Posts
what's the format of the file?

Consider using dlmread or csvread

(check the help if you don't know how).
Similar Threads
Thread Thread Starter Forum Replies Last Post
not sending DogDragon Folding@Home 9 14 Jul 2007 5:08pm
Help! Can't get rid of Qoologic trojan! metalchick666 Resolved / Inactive 14 4 Jul 2006 10:45am
F@H Stats not changing????? freya9699 Folding@Home 25 11 Oct 2005 9:42pm
What do you suggest???? chipatkinson Folding@Home 7 1 Aug 2004 1:25pm
points per WU entropy Folding@Home 18 5 May 2004 5:06am

Go Back   Icrontic Forums > Tech: Software > General Software > Matlab Help
Jump to
This Thread Search this Thread
Search this Thread:

Advanced Search


Current time: 4:39am (GMT)
Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Get Vanilla instead. Trust me.