How to create an array or a set of arrays from data files?
Ältere Kommentare anzeigen
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!
Akzeptierte Antwort
Weitere Antworten (1)
Jan
am 18 Okt. 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 Kommentare
Faisal Muhammad
am 18 Okt. 2015
Walter Roberson
am 19 Okt. 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
am 22 Okt. 2015
Bearbeitet: Faisal Muhammad
am 22 Okt. 2015
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!