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

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?

0 Comments

Accepted Answer

David Jacob on 8 Mar 2021

Direct link to this answer

Cancel Copy to Clipboard

Direct link to this answer

Cancel Copy to Clipboard 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 Comments
David Mrozek on 8 Mar 2021

Direct link to this comment

Cancel Copy to Clipboard

Direct link to this comment

Cancel Copy to Clipboard

Ok this helped me a lot. But for a better better learning experience I would like to ask you about your thought process behind it (if you don´t mind)?

a.) I do not understand the 'w' after fopen in point 1.) Is this a condition under which the txt file should be opened?? b.) In your 2nd point there is the string '%s\n', Does the % sign have another purpose expect being the indicator for a comment? This goes also for the s and \n signs? I have seen this sign at some other websites but I cannot imagine their usage. Anyway thank you very much. David Jacob on 8 Mar 2021

Direct link to this comment

Cancel Copy to Clipboard

Direct link to this comment

Cancel Copy to Clipboard

The 'w' specifies the file access type of the fopen() function. By default fopen() opens a file for reading. That's why I specified 'w' to write to the file. Once opened the file remains open in MATLAB until you close it using fclose() .

Within the fprintf() function I used a formating operator ( % ). I put %s to add the strings inside your array.

Use \n in order to go to the next row, once a row is finished. That way you prevent that all your cell elements are going to be printed into the same row in your text file.

The : o perator at the end of at the end of your cell array works like a for loop that goes through your cell array and prints each cell array at the position of your place holder %s .

I hope that makes it a little clearer. Let me know if you have any questions left! Also, check out the MATLAB documentation on the fopen() and fprintf() functions.