How to write a string matrix in to a txt file

7 views (last 30 days)
Hi, I have stored some information in to a matrix sized n x 2. All cells are individual strings. The structure is like this:
A={[filename1.txt], []
[vector1 m x 1], [vector2 m x 1
[filename2.txt], []
[vector3 p x 1], [vector4 p x 1}
etc...
So the odd row is just a txt-file name and empty string and every second row has two information vectors (same sized). How to write this down to a file row-by-row.
Thank you.
  2 Comments
Walter Roberson
Walter Roberson on 23 Nov 2017
Is your structure
A = {'filename1.txt', []; 'vector1 m x 1', 'vector2 m x 1'; ...}
or is it
A = {'filename1.txt', ''; 'vector1 m x 1', 'vector2 m x 1'; ...}
or is it
A = {"filename1.txt", ""; "vector1 m x 1", "vector2 m x 1"; ...}
or is it
A = {{'filename1.txt'}, {''}; {'vector1 m x 1'}, {'vector2 m x 1'}; ...}
or something else?
Kalle Palola
Kalle Palola on 23 Nov 2017
Edited: Walter Roberson on 23 Nov 2017
Structure is like that. It is a bit confusing to me that those "1x1 cell" cells arent always 1x1 vectors but they may be longer. And that "hidden" info is the one that interests me. Maybe this is not the wisest way to do it?
I have a loop in which I scan through a set of files for some rows and indexes. In A-matrix I store the results. Don't get confused by the first row. It's just the code structure that makes the first row empty.
So the structure is like this:
a='textfilename.txt'
b=[1 14 16 88]' %Index vector
c={'info1' 'info2' 'info3' 'info4'}' %String vector
...and then I store these in to the A
A={a, [];
b, c}
and then I pile the results in the end of the file creating new rows while we are in the loop and the results pile up.
At least the code is working and the results get stored withouth errors. But writing the results open is the problem.

Sign in to comment.

Answers (1)

KSSV
KSSV on 23 Nov 2017
fid = fopen('myfile.txt','w');
fprintf(fid,'%s\n',A{:});
fclose(fid);
  3 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!