CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

Reading data files in Matlab the Fortran way

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By kaya

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 24, 2015, 01:27
Default Reading data files in Matlab the Fortran way
  #1
New Member
 
Faisal Muhammad
Join Date: Oct 2015
Location: Canada
Posts: 9
Rep Power: 10
faysaal is on a distinguished road
Hi all..
I am doing my Masters Thesis in CFD. I have to write a code in Matlab first to generate an algebraic grid (which I have done) and then solve Euler Equation using Runge-Kutta method with TVD.

The ramp which I have to solve contains a compression corner followed by an expansion corner.

The problem which I have in Matlab is reading files into arrays.In Fortran we would see use the read/write statement simply as, for WRITING

OPEN(1,FILE=GRID.DAT)
WRITE(*,*) 'READING GRID ...'
DO 110 J=1,JM
DO 120 I=1,IM
WRITE(1,104)X(I,J),Y(I,J),XIX(I,J),XIY(I,J),
& ETAX(I,J),ETAY(I,J),JJ(I,J)
120 CONTINUE
110 CONTINUE
104 FORMAT(7(2X,D14.8))
CLOSE(1)

and for READING

OPEN(1,FILE=GRID.DAT)
WRITE(*,*) 'READING GRID ...'
DO 110 J=1,JM
DO 120 I=1,IM
READ (1,104)X(I,J),Y(I,J),XIX(I,J),XIY(I,J),
& ETAX(I,J),ETAY(I,J),JJ(I,J)
120 CONTINUE
110 CONTINUE
104 FORMAT(7(2X,D14.8))
CLOSE(1)


can someone please tell me how this can be done in Matlab?

Last edited by faysaal; October 24, 2015 at 05:21. Reason: I want to attach a file
faysaal is offline   Reply With Quote

Old   October 24, 2015, 03:37
Default
  #2
Senior Member
 
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,764
Rep Power: 71
FMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura about
load "file.dat" - ascii

is an example to read data in matlab, they will be stored in the array file.


yc = [x(1:n); y(1:n)];fid = fopen('y_x.dat','w');
fprintf(fid,'%7.4f %12.8f\n',yc); fclose(fid);

is an example of writing the vectors y and x on the file y_x.dat
FMDenaro is offline   Reply With Quote

Old   October 24, 2015, 05:04
Default
  #3
New Member
 
Faisal Muhammad
Join Date: Oct 2015
Location: Canada
Posts: 9
Rep Power: 10
faysaal is on a distinguished road
Thanks..I will give it a try and come back
faysaal is offline   Reply With Quote

Old   October 24, 2015, 05:17
Default
  #4
New Member
 
Faisal Muhammad
Join Date: Oct 2015
Location: Canada
Posts: 9
Rep Power: 10
faysaal is on a distinguished road
I have written the data in this fashion

fid = fopen('grid.txt','w');
for J=1:JM
for I=1:IM
fprintf (fid,'%12.6f%12.6f%12.6f%12.6f%12.6f%12.6f%12.6f\n ',X(I,J),Y(I,J),XIX(I,J),XIY(I,J),ETAX(I,J),ETAY(I ,J),JJ(I,J));
end
end

fclose(fid);
end

Now I want the same to be read accordingly. I will attach grid.txt for your reference that is generated by the above code. Thanks for the help.
faysaal is offline   Reply With Quote

Old   October 24, 2015, 15:05
Default
  #5
Senior Member
 
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,668
Rep Power: 65
LuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura aboutLuckyTran has a spectacular aura about
If you wrote it using fprintf the easiest way is to read it using fscanf the same way.

Another way could be to use textscan, but I think fscanf is the most straightforward.
LuckyTran is offline   Reply With Quote

Old   October 25, 2015, 01:17
Default
  #6
New Member
 
Faisal Muhammad
Join Date: Oct 2015
Location: Canada
Posts: 9
Rep Power: 10
faysaal is on a distinguished road
I am trying to solve it but there is an error which say's "sub-scripted assignment mismatch". Here is the code for reading the first array X(I,J) that is the first column of grid.txt which contains seven columns each belonging to the seven arrays.

for J=1:131
for I=1:241
X(I,J)=fscanf(fid,'%f/n',1);
end
end

I want fscanf to allocate the first value(first row first column) of grid.txt to X(1,1). The second value(second row first coulmn) to X(2,1), the third to X(3,1) and so on up till (IMth row and first column,assuming IM as a finite integer) X(IM,1). After that X(1,2),X(2,2),X(3,2)...X(IM,2),X(1,3),X(2,3)...X(I M,3)......X(IM,JM).
faysaal is offline   Reply With Quote

Old   October 25, 2015, 05:57
Default
  #7
Member
 
Kaya Onur Dag
Join Date: Apr 2013
Posts: 94
Rep Power: 13
kaya is on a distinguished road
Quote:
Originally Posted by faysaal View Post
I am trying to solve it but there is an error which say's "sub-scripted assignment mismatch". Here is the code for reading the first array X(I,J) that is the first column of grid.txt which contains seven columns each belonging to the seven arrays.

for J=1:131
for I=1:241
X(I,J)=fscanf(fid,'%f/n',1);
end
end

I want fscanf to allocate the first value(first row first column) of grid.txt to X(1,1). The second value(second row first coulmn) to X(2,1), the third to X(3,1) and so on up till (IMth row and first column,assuming IM as a finite integer) X(IM,1). After that X(1,2),X(2,2),X(3,2)...X(IM,2),X(1,3),X(2,3)...X(I M,3)......X(IM,JM).
Code:
fid = fopen('grid.txt','r');
for J=1:JM
for I=1:IM
buffer=fscanf(fid,[repmat('%f',1,7) '/n'])
X(IM,JM)=buffer(1);
Y(IM,JM)=buffer(2);
XIX(IM,JM)=buffer(3);
XIY(IM,JM)=buffer(4);
ETAX(IM,JM)=buffer(5);
ETAY(IM,JM)=buffer(6);
JJ(IM,JM)=buffer(7);
end
end
fclose(fid);
faysaal likes this.
kaya is offline   Reply With Quote

Old   October 25, 2015, 09:56
Default Thanks
  #8
New Member
 
Faisal Muhammad
Join Date: Oct 2015
Location: Canada
Posts: 9
Rep Power: 10
faysaal is on a distinguished road
Quote:
Originally Posted by kaya View Post
Code:
fid = fopen('grid.txt','r');
for J=1:JM
for I=1:IM
buffer=fscanf(fid,[repmat('%f',1,7) '/n'])
X(IM,JM)=buffer(1);
Y(IM,JM)=buffer(2);
XIX(IM,JM)=buffer(3);
XIY(IM,JM)=buffer(4);
ETAX(IM,JM)=buffer(5);
ETAY(IM,JM)=buffer(6);
JJ(IM,JM)=buffer(7);
end
end
fclose(fid);
Thanks a lot Kaya it worked like a charm by only changing (IM,JM) to (I,J) Finally i got what I needed after so many days. Thanks once again.
faysaal is offline   Reply With Quote

Reply

Tags
reading files in matlab


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Commercial meshers] Mesh conversion problem (fluent3DMeshToFoam) Aadhavan OpenFOAM Meshing & Mesh Conversion 2 March 8, 2018 01:47
[Commercial meshers] Problem converting fluent mesh vinz OpenFOAM Meshing & Mesh Conversion 28 October 12, 2015 06:37
[GAMBIT] periodic faces not matching Aadhavan ANSYS Meshing & Geometry 6 August 31, 2013 11:25
Segmentation Fault in fluent3DMeshToFoam cwang5 OpenFOAM Bugs 23 April 13, 2011 15:37
CFX11 + Fortran compiler ? Mohan CFX 20 March 30, 2011 18:56


All times are GMT -4. The time now is 23:35.