Export a matrix to a txt file

6 vues (au cours des 30 derniers jours)
Kasper
Kasper le 12 Juin 2015
Commenté : Kasper le 15 Juin 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.

Réponse acceptée

Anthony Poulin
Anthony Poulin le 12 Juin 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 commentaire
Kasper
Kasper le 15 Juin 2015
I'm not quite sure how to implement the algorithm Azzi's has posted into your code?

Connectez-vous pour commenter.

Plus de réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 12 Juin 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 le 15 Juin 2015
Thanks to both of you! I'd wish I could give credit to both of you for answering my question.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by