How to write a string matrix in to a txt file

7 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2017-11-23
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 2017-11-23
编辑:Walter Roberson 2017-11-23
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.

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2017-11-23
fid = fopen('myfile.txt','w');
fprintf(fid,'%s\n',A{:});
fclose(fid);
  3 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by