How to create an array or a set of arrays from data files?
이전 댓글 표시
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!
채택된 답변
추가 답변 (1개)
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
Faisal Muhammad
2015년 10월 18일
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
2015년 10월 22일
편집: Faisal Muhammad
2015년 10월 22일
카테고리
도움말 센터 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!