필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Faisal Muhammad
Faisal Muhammad 2015년 10월 18일
답변: Faisal Muhammad 2015년 10월 26일
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!

채택된 답변

Faisal Muhammad
Faisal Muhammad 2015년 10월 26일
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

추가 답변 (1개)

Jan
Jan 2015년 10월 18일
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
Walter Roberson
Walter Roberson 2015년 10월 19일
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 2015년 10월 22일
편집: Faisal Muhammad 2015년 10월 22일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by