Export a matrix to a txt file

7 views (last 30 days)
Kasper
Kasper on 12 Jun 2015
Commented: Kasper on 15 Jun 2015
Hello
We are trying to export two matrices to a .txt file. Tried several things but it doesn't seem to like symbolics. The code is like this:
clear all
% end points
X = [0 1];
Y = [0 0];
% intermediate point (you have to choose your own)
Xi = mean(X);
Yi = mean(Y) + 1/250;
Xa = [X(1) Xi X(2)];
Ya = [Y(1) Yi Y(2)];
t = 1:numel(Xa);
ts = linspace(min(t),max(t),numel(Xa)*10); % has to be a fine grid
xx = spline(t,Xa,ts);
yy = spline(t,Ya,ts);
plot(xx,yy); hold on; % curve
plot(X,Y,'or') % end points
plot(Xi,Yi,'xr') % intermediate point
syms L K
XA=yy*L;
YA=xx*L;
KPNR=(1:numel(Xa)*10);
key(1:numel(Xa)*10)=K;
A=[key;KPNR;XA;YA];
B=transpose(A);
BLA(1:numel(Xa)*10)=L;
z1=(1:numel(Xa)*10);
z2=(2:numel(Xa)*10+1);
H=[BLA;z1;z2];
N=transpose(H);
What we want, is to generate some keypoints for ANSYS. For the convenience, we want to include K and L, since it is commands in ANSYS. What we want is MATLAB to export the A and N matrices to two seperate .txt files without the '[ ]' but with the ','. But can't seem to find a workaround for the symbolics. We're not that experienced with Matlab unfortunately.

Accepted Answer

Anthony Poulin
Anthony Poulin on 12 Jun 2015
Hello,
I give you this short code:
fileId = fopen('MatrixA.txt', 'w'); %To create a file
txtA = char(A); %To convert the symbolic matric into char
% To do: make an algorithm to put the Matrix in the right format "formatedA = f(txtA)"
fprintf(fileId, '%s', formatedA);
fclose(fileId);
If you test the code with fprintf(fileId, '%s', txtA);, you will see that the Matrix is not written like you want. It just needs an algorithm to put the matrix in the rigth format. Tell me, if you need help to do it.
  1 Comment
Kasper
Kasper on 15 Jun 2015
I'm not quite sure how to implement the algorithm Azzi's has posted into your code?

Sign in to comment.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 12 Jun 2015
for k=1:size(N,1)
str{k,1}=[ '''' char(N(k,1)) ' ' char(N(k,2)) ' ' char(N(k,3)) '''' ]
end

Kasper
Kasper on 15 Jun 2015
Thanks to both of you! I'd wish I could give credit to both of you for answering my question.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!