Different arrays to one text file

7 ビュー (過去 30 日間)
Debbie Oomen
Debbie Oomen 2017 年 11 月 1 日
コメント済み: KL 2017 年 11 月 2 日
header1= 'Name file';
header2='Median frequencies of MVCs';
outputFile1 = fopen(fullfile(mydir1,'Results.txt'),'w');
I have two different types of arrays: one is the filename which is a character array and the other one is a number array. I want the filename to be in the left column of the text file and the median frequency in the right column with the corresponding filename. How should I go on from here? Everything I have tried gave me the error that I was using wrong matrices.. Please help
  2 件のコメント
Debbie Oomen
Debbie Oomen 2017 年 11 月 1 日
Yes this works! Now I also need to do this for a struct array and numeric array. How can I do this?
KL
KL 2017 年 11 月 2 日
My answer should work for numeric arrays as well and for structures you could use struct2cell or even struct2table and then writetable but it all comes down to how you've stacked up your data.

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

回答 (1 件)

KL
KL 2017 年 11 月 1 日
編集済み: KL 2017 年 11 月 1 日
store them in a cell array and use fprintf,
yourCell = {'a', 'b', 'c'; 1, 2, 3};
fprintf('%s %d\n',yourCell{:});
  2 件のコメント
Debbie Oomen
Debbie Oomen 2017 年 11 月 2 日
When I open the text file, it does not save it as two column next to each other with corresponding headers. How can I ensure this?
KL
KL 2017 年 11 月 2 日
see, here's a complete working example,
yourCell = {'a', 'b', 'c'; 1, 2, 3};
head = {'filename', 'values'};
fid = fopen('sample.txt','w');
fprintf(fid,'%s %s\n',head{:});
fprintf(fid,'%s %d\n',yourCell{:});
fclose(fid)
This does exactly what you ask for. You can check it by importing the data again into the workspace,
importCheck = readtable('sample.txt')

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by