|
[Sponsors] |
October 24, 2015, 02:27 |
Reading data files in Matlab the Fortran way
|
#1 |
New Member
Faisal Muhammad
Join Date: Oct 2015
Location: Canada
Posts: 12
Rep Power: 11 |
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 06:21. Reason: I want to attach a file |
|
October 24, 2015, 04:37 |
|
#2 |
Senior Member
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,897
Rep Power: 73 |
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 |
|
October 24, 2015, 06:04 |
|
#3 |
New Member
Faisal Muhammad
Join Date: Oct 2015
Location: Canada
Posts: 12
Rep Power: 11 |
Thanks..I will give it a try and come back
|
|
October 24, 2015, 06:17 |
|
#4 |
New Member
Faisal Muhammad
Join Date: Oct 2015
Location: Canada
Posts: 12
Rep Power: 11 |
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. |
|
October 24, 2015, 16:05 |
|
#5 |
Senior Member
Lucky
Join Date: Apr 2011
Location: Orlando, FL USA
Posts: 5,763
Rep Power: 66 |
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. |
|
October 25, 2015, 02:17 |
|
#6 |
New Member
Faisal Muhammad
Join Date: Oct 2015
Location: Canada
Posts: 12
Rep Power: 11 |
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). |
|
October 25, 2015, 06:57 |
|
#7 | |
Member
Kaya Onur Dag
Join Date: Apr 2013
Posts: 94
Rep Power: 13 |
Quote:
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); |
||
October 25, 2015, 10:56 |
Thanks
|
#8 | |
New Member
Faisal Muhammad
Join Date: Oct 2015
Location: Canada
Posts: 12
Rep Power: 11 |
Quote:
|
||
Tags |
reading files in matlab |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Commercial meshers] Mesh conversion problem (fluent3DMeshToFoam) | Aadhavan | OpenFOAM Meshing & Mesh Conversion | 2 | March 8, 2018 02:47 |
[Commercial meshers] Problem converting fluent mesh | vinz | OpenFOAM Meshing & Mesh Conversion | 28 | October 12, 2015 07:37 |
[GAMBIT] periodic faces not matching | Aadhavan | ANSYS Meshing & Geometry | 6 | August 31, 2013 12:25 |
Segmentation Fault in fluent3DMeshToFoam | cwang5 | OpenFOAM Bugs | 23 | April 13, 2011 16:37 |
CFX11 + Fortran compiler ? | Mohan | CFX | 20 | March 30, 2011 19:56 |