How to create an array or a set of arrays from data files?

3 views (last 30 days)
I want to read data from file generated by the following code
fid = fopen('grid.dat','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);
The .txt file contains 31571 rows and 7 columns. (IM=241,JM=131) Please help!

Accepted Answer

Faisal Muhammad
Faisal Muhammad on 26 Oct 2015
I finally got it right by taking help from another forum after so many days. Here is the code
function read_grid
IM=241;
JM=131;
fid = fopen('grid.txt','r');
for J=1:JM
for I=1:IM
buffer = fscanf(fid,[repmat('%f',1,7) '/n']);
X(I,J)=buffer(1);
Y(I,J)=buffer(2);
XIX(I,J)=buffer(3);
XIY(I,J)=buffer(4);
ETAX(I,J)=buffer(5);
ETAY(I,J)=buffer(6);
JJ(I,J)=buffer(7);
end
end
fclose(fid);
end

More Answers (1)

Jan
Jan on 18 Oct 2015
fid = fopen('grid.dat', 'r');
if fid == -1, error('Cannot open file for reading.'); end
Data = fscanf(fid, '%g');
Data = reshape(Data, 241, 131); % Or perhaps: Data.'
fclose(fid);
Now the variables should be available as rows or columns.
  3 Comments
Walter Roberson
Walter Roberson on 19 Oct 2015
Do you mean that given a value such as 0.23 you need to find the indices in X ?
If so then
d = abs(X - TargetValue);
[r, c] = find(d == min(d));
Now r and c are vectors such that for each K, X(r(K), c(K)) is as at least as close to TargetValue as any other location in X.
I return multiple locations instead of a single location because it is possible that the target value will be exactly between two X values. If you do not care about that then you can use
[~, idx] = min(d);
[r, c] = ind2sub(size(X), idx);
and that will be a single location.
Faisal Muhammad
Faisal Muhammad on 22 Oct 2015
Edited: Faisal Muhammad on 22 Oct 2015
Thanks for the help Walter but that isnt what I want. Actually I have generated grid points and those points belong to an array. Here is the part where I do the writing of arrays into a file ( I have skipped writing X(I,J) and Y(I,J)as asked in the main question because their equations cant be shown due to their large code size)
for J=1:JM
for I=1:IM
JJ(I,J)=1/(XXI(I,J)*YETA(I,J)-YXI(I,J)*XETA(I,J));
XIX(I,J)=JJ(I,J)*YETA(I,J);
XIY(I,J)=-JJ(I,J)*XETA(I,J);
ETAX(I,J)=-JJ(I,J)*YXI(I,J);
ETAY(I,J)=JJ(I,J)*XXI(I,J);
end
end
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\n',XIX(I,J),XIY(I,J),ETAX(I,J),ETAY(I,J),JJ(I,J));
end
end
fclose(fid);
JM=131 and IM=241 for the above code. Now the file generated by this code that is grid.txt contains 5 columns each column belonging to the array defined(XIX(I,J),XIY(I,J),ETAX(I,J),ETAY(I,J),JJ(I,J)). The first column stores the values of XIX(I,J) in such a fashion,as you can see from the code, that the first number of this column represents the value of X(1,1), the second X(2,1) up till X(241,1)and then X(1,2),X(2,2),X(3,2) up till X(241,2) and so on till X(241,131). This column of XIX(I,J) and all others have 31571 rows.
Now I want to read the data from this file in the same way as I have stored it. For example if want my code to get the value of say X(56,110) it should go to the exact location. This is done in Fortran quite simply by Read/Write commands. Matlab complicates it.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!