How to read a txt file in Matlab

2 vues (au cours des 30 derniers jours)
Muhsin ACAR
Muhsin ACAR le 31 Déc 2016
Hello I a new user. I have a txt file and I need to make some changes in that file. There are some node numbers and theirs x,y,z coordinatescin it. I will seperate the nodes in compliance with their Heights and put them in groups. Could you please help me? Thank you. Happy new year.
Best regards.
Muhsin
  2 commentaires
Walter Roberson
Walter Roberson le 31 Déc 2016
Please post a sample file.
Muhsin ACAR
Muhsin ACAR le 1 Jan 2017
Dear Walter; In this file, There are a bunch of nodes (nid) with x,y,and z coordinates. 'Y' column means the height of the nodes. I do need to seperate the nodes with a tolerance value of 4.2 .For example, for the height of 91.8, the code needs to create a group for this height with the nodes having height between 91.8 + 4.2 and 91.8-4.2. The file is attached. Thank you . Happy new year. Best wishes..
Muhsin A

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 1 Jan 2017
For reading the data:
fid = fopen('cfrd.txt');
data_cell = textscan(fid, '%f%f%f%f%*f%*f', 'CollectOutput', 1, 'CommentStyle', {'$# LS-DYNA', '$# nid x y z tc rc'});
fclose(fid)
data = data_cell{1};
Now data will be an N x 4 array, with the first column being the node ID (I don't know if you need that.)
I am working on good ways to do the grouping.
  3 commentaires
Walter Roberson
Walter Roberson le 1 Jan 2017
y = data(:,3);
d = squareform( pdist(y) );
match = d <= 4.2;
[r, c] = find(match);
scatter(c, r, 1)
This shows a visual representation of where the points are within 4.2 of each other.
Now if you pick an arbitrary column,
idx = find(match(:,1000));
[r,c] = find(~match(idx,idx));
idx1 = idx(r(1)); idx2 = idx(c(1)); %276 and 137
you will find that
match(idx1,1000)
match(idx2,1000)
are both true, so both points are with distance 4.2 of point #1000, but you will find that
match(idx1, idx2)
is false, so the points are not within distance 4.2 of each other.
That tells you that you cannot do clustering. If point 276 is close enough to point 1000, and point 137 is close enough to point 1000, then you would expect that they should both be in the same cluster as point 1000, but to be in that cluster the distance between 276 and 137 would have to be <= 4.2, which is not the case.
You can find all of the distinct groupings by using a graph theory routine to find the Maximal Cliques on the match matrix (after removing self-edges)
m2 = match - diag(diag(match)); %remove self-edges
MC = maximalCliques(m2);
... it won't be fast.
Walter Roberson
Walter Roberson le 1 Jan 2017
To understand the output of
MC = maximalCliques(m2);
Each column, K, corresponds to one clique. find(MC(:,K)) are the nodes that are involved in the clique -- that is, all of those nodes are within 4.2 of each other. The find() results give meaningful index numbers into the rows of the data array.
For any given node number, N, find(MC(N,:)) tells which cliques the node is involved with. The find() results here do not have any relationship to the row numbers of the data array: they are arbitrary numbering. The important bit at the moment is that there are rows which participate in more than one clique. That is, there are multiple distinct ways of partitioning the nodes up into subsets that are each within 4.2 of each other. Some of the nodes are involved in up to 45 different subsets. So you cannot even come close to a simple clustering of nodes to each be within 4.2 of each other.

Connectez-vous pour commenter.

Plus de réponses (1)

Muhsin ACAR
Muhsin ACAR le 2 Jan 2017
Dear Walter, I would like to create different groups of nodes in compliance with their 'y' values. I have prepared a txt file for layers I want nodes to be in. the file is attached. would you please help me create the code for it? Thank you. Best wishes.
Muhsin A
  1 commentaire
Walter Roberson
Walter Roberson le 2 Jan 2017
You can use histcounts() and take the third output to get the "bin number" that would convert readily to the layer number. If you are using an older MATLAB then use the second output of histc() instead of using histcounts()

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by