필터 지우기
필터 지우기

Export a matrix to a txt file

조회 수: 5 (최근 30일)
Kasper
Kasper 2015년 6월 12일
댓글: Kasper 2015년 6월 15일
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.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

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

Kasper
Kasper 2015년 6월 15일
Thanks to both of you! I'd wish I could give credit to both of you for answering my question.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by