MATLAB Answers

How to convert the output of this coding to txt file formate using Matlab?

15 views (last 30 days)
M.S. Khan
M.S. Khan on 31 May 2020
Edited: dpb on 2 Jun 2020
HI respected MatLab community members,
could anyone help how to generate text file of it.
Thanks in advance for all kind cooperation and support.
Regards,
R1 = 16; R2 = 12; R3 = 8; C1=[10 12 14]; C2=[28 18 20]; C3 = [40 24 26];
figure(1)
numPoints = 10000;
r = randn(3, numPoints);
r = bsxfun(@rdivide, r, sqrt(sum(r.^2,1)));
r = R1 * r;
x1=C1(1,1);
y1=C1(1,2);
z1=C1(1,3);
Xs1 = r(1,:) + x1 ; % Extract Xs1 from row #1.
Ys1 = r(2,:) + y1; % Extract Ys1 from row #2.
Zs1 = r(3,:) + z1; % Extract Zs1 from row #3.
% Display the shell of points
numPoints = 10000;
r = randn(3, numPoints);
r = bsxfun(@rdivide, r, sqrt(sum(r.^2,1)));
r = R2 * r;
x2 = C2(1,1);
y2 = C2(1,2);
z2 = C2(1,3);
Xs2 = r(1,:) + x2; % Extract Xs2 from row #1.
Ys2 = r(2,:) + y2; % Extract Ys2 from row #2.
Zs2 = r(3,:) + z2; % Extract Zs2 from row #3.
hold on
numPoints = 10000;
r = randn(3, numPoints);
r = bsxfun(@rdivide, r, sqrt(sum(r.^2,1)));
r = R3 * r;
x3 = C3(1,1);
y3 = C3(1,2);
z3 = C3(1,3);
Xs3 = r(1,:) + x3; % Extract x from row #1.
Ys3 = r(2,:) + y3; % Extract y from row #2.
Zs3 = r(3,:) + z3; % Extract z from row #3.
hold on
axis square;
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
zlabel('Z', 'FontSize', 20);
%Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
hold off
axis square;
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
zlabel('Z', 'FontSize', 20);
%Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
hold off
ix1 = (Xs1-x2).^2+(Ys1-y2).^2+(Zs1-z2).^2 < R2^2;
scatter3(Xs1(~ix1),Ys1(~ix1),Zs1(~ix1),20,'r','h','filled','MarkerFaceColor',[1 0 0])
hold on
ix2 = (Xs2-x1).^2+(Ys2-y1).^2+(Zs2-z1).^2 < R1^2;
scatter3(Xs2(~ix2),Ys2(~ix2),Zs2(~ix2),20,'r','h','filled', 'MarkerFaceColor',[1 0 0])
hold on
ix3 = (Xs3-x2).^2+(Ys3-y2).^2+(Zs3-z2).^2 < R2^2;
scatter3(Xs3(~ix3),Ys3(~ix3),Zs3(~ix3),20,'r','h','filled', 'MarkerFaceColor',[1 0 0])
hold on
ix4= (Xs2-x3).^2+(Ys2-y3).^2+(Zs2-z3).^2 < R3^2;
scatter3(Xs2(~ix4),Ys2(~ix4),Zs2(~ix4),20,'r','h','filled', 'MarkerFaceColor',[1 0 0])

  5 Comments

Show 2 older comments
dpb
dpb on 31 May 2020
Just use copyfile to make a copy of it with some other file name. You're making this much more difficult than it is...you ALREADY HAVE A FILE that is text. If you want another copy by a different name/extension, then just make a copy of the m-file.
M.S. Khan
M.S. Khan on 31 May 2020
Thanks dpb for your feedback.
i am sorry that i could not convey my message in a proper way. i dont want whole file to be converted into txt formate using copyfile function.
i want the output generated (i.e. three instersected spheres) from the following code to convert into txt formate.
The txt file will contain all the xyz coordinates which i need for further calcuations.
Regards
ix1 = (Xs1-x2).^2+(Ys1-y2).^2+(Zs1-z2).^2 < R2^2;
scatter3(Xs1(~ix1),Ys1(~ix1),Zs1(~ix1),20,'r','h','filled','MarkerFaceColor',[1 0 0])
hold on
ix2 = (Xs2-x1).^2+(Ys2-y1).^2+(Zs2-z1).^2 < R1^2;
scatter3(Xs2(~ix2),Ys2(~ix2),Zs2(~ix2),20,'r','h','filled', 'MarkerFaceColor',[1 0 0])
hold on
ix3 = (Xs3-x2).^2+(Ys3-y2).^2+(Zs3-z2).^2 < R2^2;
scatter3(Xs3(~ix3),Ys3(~ix3),Zs3(~ix3),20,'r','h','filled', 'MarkerFaceColor',[1 0 0])
hold on
ix4= (Xs2-x3).^2+(Ys2-y3).^2+(Zs2-z3).^2 < R3^2;
scatter3(Xs2(~ix4),Ys2(~ix4),Zs2(~ix4),20,'r','h','filled', 'MarkerFaceColor',[1 0 0])
dpb
dpb on 31 May 2020
Oh. That's different Q?, indeed! OK, sorry, language sometimes gets in the way, granted. No problem, glad to help once know what the real Q? is...
You haven't saved the output to write, locally, it is in the X/Y/ZData arrays of the figure, but that's not easiest way to get at it. What you need to do is to use temporary variable(s). I'll add some code to Answer now we've got a target to shoot at...

Sign in to comment.

Accepted Answer

dpb
dpb on 31 May 2020
Edited: dpb on 2 Jun 2020
ix1 = (Xs1-x2).^2+(Ys1-y2).^2+(Zs1-z2).^2 < R2^2;
scatter3(Xs1(~ix1),Ys1(~ix1),Zs1(~ix1),20,'r','h','filled','MarkerFaceColor',[1 0 0])
hold on
ix2 = (Xs2-x1).^2+(Ys2-y1).^2+(Zs2-z1).^2 < R1^2;
scatter3(Xs2(~ix2),Ys2(~ix2),Zs2(~ix2),20,'r','h','filled', 'MarkerFaceColor',[1 0 0])
hold on
ix3 = (Xs3-x2).^2+(Ys3-y2).^2+(Zs3-z2).^2 < R2^2;
scatter3(Xs3(~ix3),Ys3(~ix3),Zs3(~ix3),20,'r','h','filled', 'MarkerFaceColor',[1 0 0])
hold on
ix4= (Xs2-x3).^2+(Ys2-y3).^2+(Zs2-z3).^2 < R3^2;
scatter3(Xs2(~ix4),Ys2(~ix4),Zs2(~ix4),20,'r','h','filled', 'MarkerFaceColor',[1 0 0])
Your code above is plotting the four sets of arrays you're selecting in the scatter3 calls -- but to write those data out you need to have saved them to local variables (or duplicate the selection again). Depending on the size of the arrays, this could create a very large file, indeed.
Do you really, really have to have a text file -- could you not use a MATLAB .mat file instead? It will have full precision of the results and be smaller. Other results will generally only have 6 or 7 digits of precision; if this is ok, then ok, but just need be aware.
ix1 = (Xs1-x2).^2+(Ys1-y2).^2+(Zs1-z2).^2 < R2^2; % select the boundary range set
X=(Xs1(~ix1); Y=Ys1(~ix1); Z=Zs1(~ix1); % save temporary subset
scatter3(X,Y,Z,20,'r','h','filled','MarkerFaceColor',[1 0 0]) % plot that set
writematrix([X(:),Y(:),Z(:)],'YourFileName1.txt') % write this set to a file.
Lather, rinse and repeat for the other sets...NB: you MUST create a new and unique filename for each of the sets; I just used a sample placeholder above. Use fulfile to build a fully-qualified filename if writing to somewhere other than the current working directory.

  1 Comment

M.S. Khan
M.S. Khan on 2 Jun 2020
Thanks dpb, really appreciated. It was excellent feedback.
I had to install the Matlab2020 version to perform writematrix() functions cause i was 2018 version.
Regards

Sign in to comment.

More Answers (0)


Translated by