Arrays 第一列中的单元格数组,文本文件中其他列中的矩阵。怎样
有了这些,我就有了相关的数据:Arrays 第一列中的单元格数组,文本文件中其他列中的矩阵。怎样,arrays,matlab,file,text,matrix,Arrays,Matlab,File,Text,Matrix,有了这些,我就有了相关的数据: A = {'Subject 1'; 'Subject 2'; 'Subject 3'; 'Subject 4'; 'Subject 5'} 我想以这种格式将数据输出到.txt文件中: ID = [1,2,3,4,5] Score = [65, 90, 42, 53, 13] 我知道我需要先转换数据谢谢你rayryeng Screen Name ID Score Subject 1 1 65 Subj
A = {'Subject 1'; 'Subject 2'; 'Subject 3'; 'Subject 4'; 'Subject 5'}
我想以这种格式将数据输出到.txt文件中:
ID = [1,2,3,4,5]
Score = [65, 90, 42, 53, 13]
我知道我需要先转换数据谢谢你rayryeng
Screen Name ID Score
Subject 1 1 65
Subject 2 2 90
Subject 3 3 42
Subject 4 4 53
Subject 5 5 13
因此,我刚刚用相关错误更新了以下代码:
谢谢Robert p:它不再崩溃,但是它不会输入第二行fprintf的数据。第一行fprintf很好,但是
我必须如何写出fprintf以将单元格数组合并到第一列中,并将矩阵合并到后续列中 看看这是否适合你-
fileID = fopen('user_Database.txt', 'w');
fprintf(fileID,'%8s %16s %24s\n', 'Screen Name', 'ID', 'Score');
fprintf(fileID,'%8s %16.2f %24.2f\n', A{:}, ID, Score);
代码运行-
%// Inputs (slightly modified from original ones to show spacings are taken care of)
A = {'Subject 1'; 'Subject 2'; 'Subject 3'; 'Subject 4'; 'Subject 5'}
ID = [101,2,3,201,8]
Score = [165, 2090, 42, 53, 13]
%// Create cell array versions of ID and Score to account for proper
%// spacings between the prints of ID and Score
Score_cell = cellstr(num2str(Score.')); %//'
ID_cell = cellstr(num2str(ID.')); %//'
output_file = 'results.txt'; %//'# Edit if needed to be saved to a different path
fid = fopen(output_file, 'w+'); %// open file for writing
fprintf(fid, 'Screen Name\t ID\tScore\n'); %// write the header line
for ii=1:numel(A)
fprintf(fid, '%s\t %s\t %s\n',A{ii}, ID_cell{ii},Score_cell{ii}); %// write data
end
fclose(fid); %// close file as writing is done
它不再给出错误,但它不会打印出数据。这只是打印出上面贴的第一行fprintf。不管怎样,如果有问题,我可以回到这里。我试图用更多的列测试代码。非常感谢。
%// Inputs (slightly modified from original ones to show spacings are taken care of)
A = {'Subject 1'; 'Subject 2'; 'Subject 3'; 'Subject 4'; 'Subject 5'}
ID = [101,2,3,201,8]
Score = [165, 2090, 42, 53, 13]
%// Create cell array versions of ID and Score to account for proper
%// spacings between the prints of ID and Score
Score_cell = cellstr(num2str(Score.')); %//'
ID_cell = cellstr(num2str(ID.')); %//'
output_file = 'results.txt'; %//'# Edit if needed to be saved to a different path
fid = fopen(output_file, 'w+'); %// open file for writing
fprintf(fid, 'Screen Name\t ID\tScore\n'); %// write the header line
for ii=1:numel(A)
fprintf(fid, '%s\t %s\t %s\n',A{ii}, ID_cell{ii},Score_cell{ii}); %// write data
end
fclose(fid); %// close file as writing is done
>> type results.txt
Screen Name ID Score
Subject 1 101 165
Subject 2 2 2090
Subject 3 3 42
Subject 4 201 53
Subject 5 8 13