How to create an array or a set of arrays from data files?
Afficher commentaires plus anciens
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!
Réponse acceptée
Plus de réponses (1)
Jan
le 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 commentaires
Faisal Muhammad
le 18 Oct 2015
Walter Roberson
le 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
le 22 Oct 2015
Modifié(e) : Faisal Muhammad
le 22 Oct 2015
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!