gpt4 book ai didi

matlab - 使用 cell2mat 将数字矩阵与字符串向量(列标签)连接起来的问题

转载 作者:太空宇宙 更新时间:2023-11-03 19:38:44 25 4
gpt4 key购买 nike

我是 Mac 用户 (10.6.8),使用 MATLAB 处理计算结果。我将大量数字表输出到 .csv 文件。然后我在 EXCEL 中使用 .csv 文件。这一切都很好。

问题是每一列数字都需要一个标签(一个字符串标题)。我不知道如何将标签连接到数字表。如果有任何建议,我将不胜感激。以下是一些可能有用的进一步信息:

我的标签包含在一个元胞数组中:

    columnsHeader = cell(1,15)

我填写的是计算结果;例如:

    columnsHeader{1}  = propertyStringOne (where propertyStringOne = 'Liq')

每次计算的标签顺序都不同。我的第一次尝试是尝试直接连接标签:

    labelledNumbersTable=cat(1,columnsHeader,numbersTable)

我收到一条错误消息,指出串联的类型必须相同。所以我尝试使用 cell2mat 转换标签/字符串:

    columnsHeader = cell2mat(columnsHeader);
labelledNumbersTable = cat(1,columnsHeader,numbersTable)

但这把所有单独的标签都变成了一个长词......这导致:

??? Error using ==> cat

CAT arguments dimensions are not consistent.

有谁知道可以让我保留原始标签元胞数组的替代方法吗?

最佳答案

您将不得不以两种不同的方式处理将列标题和数字数据写入文件。必须使用 FPRINTF 来输出字符串元胞数组。功能,如 this documentation for exporting cell arrays to text files 中所述.然后,您可以使用函数 DLMWRITE 将数字数据附加到文件(已包含列标题)来输出数字数据。 .这是一个例子:

fid = fopen('myfile.csv','w');              %# Open the file
fprintf(fid,'%s,',columnsHeader{1:end-1}); %# Write all but the last label
fprintf(fid,'%s\n',columnsHeader{end}); %# Write the last label and a newline
fclose(fid); %# Close the file
dlmwrite('myfile.csv',numbersTable,'-append'); %# Append your numeric data

关于matlab - 使用 cell2mat 将数字矩阵与字符串向量(列标签)连接起来的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6538756/

25 4 0