matlab - 将 fprintf 与双数组和元胞数组结合使用

标签 matlab cell-array

使用 fprintf 我想生成如下所示的输出:

names abc and numbers 1
names def and numbers 2
names ghi and numbers 3

这是我尝试使用的代码:

names= {'abc','def','ghi'}
numbers = [1 2 3];
fprintf('names %s and numbers %2.2f \n',names{1:3},numbers)

不幸的是,它产生的输出看起来像这样:

names abc and numbers 100.00
names ef and numbers 103.00
names hi and numbers 1.00
names and number

有谁知道如何解决这个问题?或者甚至可以将 fprintf 与元胞数组结合使用?提前致谢

最佳答案

看看您传递给 fprintf 的内容,它只是顺序错误,数字创建一个参数而不是三个单独的参数:

>> names{1:3},numbers

ans =

abc


ans =

def


ans =

ghi


numbers =

     1     2     3

改为使用:

C=names
C(2,:)=num2cell(numbers)
fprintf('names %s and numbers %2.2f \n',C{:})

如果您输入 C{:},您将按顺序看到各个参数:

>> fprintf('names %s and numbers %2.2f \n',C{:})
names abc and numbers 1.00 
names def and numbers 2.00 
names ghi and numbers 3.00 

>> C{:}

ans =

abc


ans =

     1


ans =

def


ans =

     2


ans =

ghi


ans =

     3

关于matlab - 将 fprintf 与双数组和元胞数组结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486219/

相关文章:

matlab - 比较matlab中的两个元胞数组元素

arrays - 使用 Octave 计算数组中每个唯一向量的实例数的最有效方法是什么?

matlab - 如何让 fprintf 为空数值保留空间?

arrays - Matlab 数组具有不同数据类型的列?

matlab - 在 MATLAB 中用元胞数组中的零替换 NaN

arrays - 用于绘图的元胞数组到数值数组

matlab - 在不同大小的不同单元格数组中查找重复值的最快方法

c++ - 如何在Matlab中保存多维矩阵的二进制并将其加载为C++结构?

Matlab - 直接绘图到 .ps 文件

matlab - 从Matlab中的矩阵中删除行和列