write cell array of strings in text file

I would like to save my cell array of strings in a *.txt file. A simple way is like the following:
filePh = fopen('features_.txt','w');
[rows,cols]=size(Strings);
for r=1:rows
fprintf(filePh,'%s\n',Strings{r,1};
end
So, each cell that is a string (like a sentence...) in one row. But some strings are not very well defined. for example in one string there might be two whitespace between words, or even a tab. Then this code separates the strings in to different rows.
Does anyone know the solution?
Thank you very much in advance

 採用された回答

Kirby Fears
Kirby Fears 2016 年 4 月 15 日

8 投票

filePh = fopen('features.txt','w');
fprintf(filePh,'%s\n',Strings{:});
fclose(filePh);

8 件のコメント

sh as
sh as 2016 年 4 月 15 日
編集済み: sh as 2016 年 4 月 15 日
Thank you for the reply.
I am struggling with different type of files for a simple task, as you see...
This code still did not solve the problem. An example is the rows 6625,6631... in the attached file...
Kirby Fears
Kirby Fears 2016 年 4 月 15 日
Your cell has 2 columns. Your original question was stated for a single column. For the attached data:
features_score = features_score';
filePh = fopen('features.csv','w');
fprintf(filePh,'%s,%s\n',features_score{:});
fclose(filePh);
sh as
sh as 2016 年 4 月 15 日
Thanks a lot for reply.
It seems I am going toward the solution.
In fact I would like to save the whole of cell array "features_score" in to *xls/csv file, in which first column of file is for strings and the second column is numeric values. beside your solution, there is another problem that some strings do not fit in one row and take 2 rows...
Kirby Fears
Kirby Fears 2016 年 4 月 15 日
編集済み: Kirby Fears 2016 年 4 月 15 日
You're data really needs to be cleaned before exporting to a file. There are lots of double spaces and tabs that create accidental line breaks in fprintf.
Here are two extra lines to remove double spaces, tabs, and leading/trailing white space:
cleanData = features_score';
cleanData = regexprep(cleanData,'(\s|\t)+',' ');
cleanData = strtrim(cleanData);
filePh = fopen('features.csv','w');
fprintf(filePh,'%s,%s\n',cleanData{:});
fclose(filePh);
sh as
sh as 2016 年 4 月 15 日
Ah, thank you very much. It worked. Just I moved this code to *.txt file, that is OK.
But, If I want to save them in csv file, then the 2 columns of my cell array, are just added in one column in csv file. I mean they are not in a separate columns in *.csv ...
Kirby Fears
Kirby Fears 2016 年 4 月 15 日
編集済み: Kirby Fears 2016 年 4 月 15 日
Perhaps .csv is ambiguous in your operating system, I don't know.
When I write to .csv, it works fine. The columns are separated by a comma. It opens in Excel as two separate columns.
sh as
sh as 2016 年 4 月 15 日
Strange! and, thanks a lot for your help.
sh as
sh as 2016 年 4 月 15 日
Well, for me ';' separated the columns

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2016 年 4 月 15 日

コメント済み:

2016 年 4 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by