From: Sydney Hai on 16 Jun 2010 15:26 Hi all, I have been troubled with this problem using for loop, hope I can get some help. I am trying to modify a text file with columns of strings and numeric values. Here I am extracting XYZ coordinates from a file and then do calculation with all 64 rows of XYZ and find out the four smallest distances from A(X,Y,Z) to B(X1,Y1,Z1) i used for v=1:64 fid=fopen('FILE_Si.m'); C=textscan(fid, '%*s %*s %f %f %f %*[^\n]', 64); X=C{1,1}(v); Y=C{1,2}(v); Z=C{1,3}(v); end to read the coordinates in the file, X, Y, and Z for all 64 rows of the file. first of all, is there a way to save the XYZ values I get in the loop to excel or something so I don't end up with only one value in the end? because each new value overwrites the previous one. I tried saving all the variables, but then I wouldn't be able to do calculations with all of those or sort them cuz of the variable names. Then I thought I could do the calculation in the for loop, so when doing the calculation of the distance of this point with another point (X1,Y1,Z1), i used distance=sqrt((X-X1)^2+(Y-Y1)^2+(Z-Z1)^2; i wish to have all the distances from the 64 calculations so I can sort them in the end to get the four smallest distance values. I did [B, IX]=sort(distance); so once I get the smallest value I'll be able to know the index of the XYZ of that min distance. but B only contains one value so the sorting is not working. One last question is, can I set a condition in for loop, that when the A (X, Y, Z) value is the same as C(X2,Y2,Z2), eliminate that A point in the for loop data and the rest of the calculation? I tried, for v=1:64, if v=h(my C point), break, end; but it wouldn't work. any idea how I can solve these problems? It's been troubling me for days, thank you all so much! Best, Sydney
From: us on 16 Jun 2010 15:46 "Sydney Hai" <xueyinghai(a)gmail.com> wrote in message <hvb8gs$jt7$1(a)fred.mathworks.com>... > Hi all, > > I have been troubled with this problem using for loop, hope I can get some help. > > I am trying to modify a text file with columns of strings and numeric values. > Here I am extracting XYZ coordinates from a file and then do calculation with all 64 rows of XYZ and find out the four smallest distances from A(X,Y,Z) to B(X1,Y1,Z1) > > i used > for v=1:64 > fid=fopen('FILE_Si.m'); > C=textscan(fid, '%*s %*s %f %f %f %*[^\n]', 64); > X=C{1,1}(v); > Y=C{1,2}(v); > Z=C{1,3}(v); > end > to read the coordinates in the file, X, Y, and Z for all 64 rows of the file. > first of all, is there a way to save the XYZ values I get in the loop to excel or something so I don't end up with only one value in the end? because each new value overwrites the previous one. > I tried saving all the variables, but then I wouldn't be able to do calculations with all of those or sort them cuz of the variable names. > > Then I thought I could do the calculation in the for loop, so when doing the calculation of the distance of this point with another point (X1,Y1,Z1), i used > distance=sqrt((X-X1)^2+(Y-Y1)^2+(Z-Z1)^2; > i wish to have all the distances from the 64 calculations so I can sort them in the end to get the four smallest distance values. > I did [B, IX]=sort(distance); so once I get the smallest value I'll be able to know the index of the XYZ of that min distance. > but B only contains one value so the sorting is not working. > > One last question is, can I set a condition in for loop, that when the A (X, Y, Z) value is the same as C(X2,Y2,Z2), eliminate that A point in the for loop data and the rest of the calculation? > I tried, for v=1:64, if v=h(my C point), break, end; > but it wouldn't work. > > any idea how I can solve these problems? It's been troubling me for days, thank you all so much! > > Best, > Sydney this is awkward... a hint: - read the content of your file at once into row-vecs X/Y/Z... - then compute your distance profile... - and save your results using help xlswrite; us
From: Sydney Hai on 16 Jun 2010 16:09 "us " <us(a)neurol.unizh.ch> wrote in message <hvb9ls$3ms$1(a)fred.mathworks.com>... > "Sydney Hai" <xueyinghai(a)gmail.com> wrote in message <hvb8gs$jt7$1(a)fred.mathworks.com>... > > Hi all, > > > > I have been troubled with this problem using for loop, hope I can get some help. > > > > I am trying to modify a text file with columns of strings and numeric values. > > Here I am extracting XYZ coordinates from a file and then do calculation with all 64 rows of XYZ and find out the four smallest distances from A(X,Y,Z) to B(X1,Y1,Z1) > > > > i used > > for v=1:64 > > fid=fopen('FILE_Si.m'); > > C=textscan(fid, '%*s %*s %f %f %f %*[^\n]', 64); > > X=C{1,1}(v); > > Y=C{1,2}(v); > > Z=C{1,3}(v); > > end > > to read the coordinates in the file, X, Y, and Z for all 64 rows of the file. > > first of all, is there a way to save the XYZ values I get in the loop to excel or something so I don't end up with only one value in the end? because each new value overwrites the previous one. > > I tried saving all the variables, but then I wouldn't be able to do calculations with all of those or sort them cuz of the variable names. > > > > Then I thought I could do the calculation in the for loop, so when doing the calculation of the distance of this point with another point (X1,Y1,Z1), i used > > distance=sqrt((X-X1)^2+(Y-Y1)^2+(Z-Z1)^2; > > i wish to have all the distances from the 64 calculations so I can sort them in the end to get the four smallest distance values. > > I did [B, IX]=sort(distance); so once I get the smallest value I'll be able to know the index of the XYZ of that min distance. > > but B only contains one value so the sorting is not working. > > > > One last question is, can I set a condition in for loop, that when the A (X, Y, Z) value is the same as C(X2,Y2,Z2), eliminate that A point in the for loop data and the rest of the calculation? > > I tried, for v=1:64, if v=h(my C point), break, end; > > but it wouldn't work. > > > > any idea how I can solve these problems? It's been troubling me for days, thank you all so much! > > > > Best, > > Sydney > > this is awkward... > a hint: > - read the content of your file at once into row-vecs X/Y/Z... > - then compute your distance profile... > - and save your results using > > help xlswrite; > > us Hi, Thanks so much for the prompt reply! One question, I'm calculating the distance of one point with all the 63 others, would i be able to do that without a for loop? or how can i save all the distance results and then sort? I appreciate it! Sydney
From: us on 16 Jun 2010 16:19 "Sydney Hai" > One question, I'm calculating the distance of one point with all the 63 others, would i be able to do that without a for loop? or how can i save all the distance results and then sort? > > I appreciate it! > Sydney one of the solutions % the data x=[0,1,4,6,-1]; y=[0,1,2,2,2]; x0=0; y0=0; % the engine d2=(x-x0).^2+(y-y0).^2; % <- use this if you only want to sort, etc... d=sqrt(d2); % <- use this to get the actual distance... us
From: dpb on 16 Jun 2010 16:26 Sydney Hai wrote: .... > Thanks so much for the prompt reply! One question, I'm calculating the > distance of one point with all the 63 others, ... Is that 63 distances from a selected point or the combination of all possible distances between the full vector of 63 locations? --
|
Next
|
Last
Pages: 1 2 Prev: How to divide a line in equal section? Next: Fix for Isosurface problems |