使用cell2mat将数字矩阵与字符串向量(列标签)连接在一起 [英] Problem concatenating a matrix of numbers with a vector of strings (column labels) using cell2mat

查看:701
本文介绍了使用cell2mat将数字矩阵与字符串向量(列标签)连接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a Mac user (10.6.8) using MATLAB to process calculation results. I output large tables of numbers to .csv files. I then use the .csv files in EXCEL. This all works fine.

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

The problem is that each column of numbers needs a label (a string header). I can't figure out how to concatenate labels to the table of numbers. I would very much appreciate any advice. Here is some further information that might be useful:

我的标签包含在单元格数组中:

My labels are contained within a cell array:

    columnsHeader = cell(1,15)



计算结果;例如:

that I fill in with calculation results; for example:

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

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

The sequence of labels is different for each calculation. My first attempt was to try and concatenate the labels directly:

    labelledNumbersTable=cat(1,columnsHeader,numbersTable)

我收到一个错误,连接类型需要是相同的。所以我尝试使用cell2mat转换标签/字符串:

I received an error that concatenated types need to be the same. So I tried converting the labels/strings using cell2mat:

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

并将它们变成一个长字...这导致:

But that took ALL the separate labels and made them into one long word... Which leads to:


?使用==> cat时出错

??? Error using ==> cat

CAT参数维度不一致。

CAT arguments dimensions are not consistent.

有没有人知道一个替代方法,将允许我保留我的原始单元格数组的标签?

Does anyone know of an alternative method that would allow me to keep my original cell array of labels?

推荐答案

您必须处理以两种不同的方式将列标题和数字数据写入文件。输出字符串的单元格数组必须使用 FPRINTF 函数完成,如本文档中所述,用于将单元格阵列导出到文本文件。然后,您可以使用函数 DLMWRITE 。这里有一个例子:

You will have to handle writing the column headers and the numeric data to the file in two different ways. Outputting your cell array of strings will have to be done using the FPRINTF function, as described in this documentation for exporting cell arrays to text files. You can then output your numeric data by appending it to the file (which already contains the column headers) using the function DLMWRITE. Here's an example:

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

这篇关于使用cell2mat将数字矩阵与字符串向量(列标签)连接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆