필터 지우기
필터 지우기

How to convert and write nested cell arrays into a TXT file without adding extra quotes?

조회 수: 13 (최근 30일)
Hello everyone,
I wanted to convert a (293 x 1) cell containing only one datatype (string) into a TXT file. The TXT file should look exactly like the plane.mat file without any additional " " quotes. I wanted to achieve this with the following command:
writecell(CATIA_EBENE_MAKRO, 'Test.txt')
However once I open the TXT file I have some issues with the representation:
1.) I do not know how to get rid of the additional " " quotes from the string data type
2.) In some lines the " " quotes are strangely adding up by one in the TXT file compared to the original plane.mat file:
'Set hybridShapeSpline11= hybridShapes1.Item("Spline.1")' % e.g 11th row of the 293 x 1 cell in the plane.met file
"'Set hybridShapeSpline11= hybridShapes1.Item(""Spline.1"")'" % e.g 11th in the TXT File after the conversion
% Notice: The "Spline.1" is intended. It just bother me that after that the TXT file (after conversion) shows ""Spline.1""
So I would like to ask you if I could, e.g with the cellfun command, get rid of the outer " " quotes as well as solve the problem with the "" "" double quotes? Since I am not familiar with the data export to a different file I would like to ask you if you have a code in mind?

채택된 답변

David Jacob
David Jacob 2021년 3월 8일
Hey,
You can use fopen() and fprintf() for that.
  1. Open your the file you want to write to in writing mode: f=fopen('Test.txt','w')
  2. Add each row of that cell array as a string to your file: fprintf(f,'%s\n',CATIA_EBENE_MAKRO{:})
  3. Make sure to close the file: fclose(f)
  댓글 수: 5
Rik
Rik 2021년 3월 8일
편집: Rik 2021년 3월 8일
Just a clarifying note:
data{:}
will generate a comma separated list. In the fprintf call it would be equivalent of doing this (assuming data is a 3 element array):
fprintf('%s\n',data{1},data{2},data{3})
You can also use this in other circumstances, like cat. I also sometimes use it to generate multidimentional arrays:
A=cell(1,3);
[A{:}]=ndgrid(1:3);
celldisp(A)
A{1} = (:,:,1) = 1 1 1 2 2 2 3 3 3 (:,:,2) = 1 1 1 2 2 2 3 3 3 (:,:,3) = 1 1 1 2 2 2 3 3 3 A{2} = (:,:,1) = 1 2 3 1 2 3 1 2 3 (:,:,2) = 1 2 3 1 2 3 1 2 3 (:,:,3) = 1 2 3 1 2 3 1 2 3 A{3} = (:,:,1) = 1 1 1 1 1 1 1 1 1 (:,:,2) = 2 2 2 2 2 2 2 2 2 (:,:,3) = 3 3 3 3 3 3 3 3 3
And I absolutely agree with the advice to read the documentation. It is one of the major advantages of Matlab over the competition.
Stephen23
Stephen23 2021년 3월 9일
Prefer to open the file in text mode too, which will neatly handle the EOL conversion for your OS:
fid = fopen('Test.txt','wt')
% ^ text mode
Useful information on comma-separated lists:

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by