问题描述
Octave 6.1.0 (GUI)
这是 Octave: How to turn a vector of integers into a cell array of strings? 的衍生产品。
>> a = 1:3;
>> cellstr(int2str(a(:)))
ans =
{
[1,1] = "[square]"
}
虽然输出应该是:
ans =
{
[1,1] = 1
[2,1] = 2
[3,1] = 3
}
如何解决这个问题?
ps:仅供参考,不作为答案,没有此问题的解决方法是 cellstr(num2str(a(:)))
。
解决方法
这是由一个可怕的初学者错误造成的。
问题下的第一个帮助评论已经指明了方向。
-
如果在线 Octave 与您的 GUI 完全不同,那可能是您的错。
-
为了发现问题,请检查可能导致差异的函数:
>> which('num2str')
'num2str' 是文件 C:\Users\USERNAME\AppData\Local\Programs\GNU Octave\Octave-6.1.0\mingw64\share\octave\6.1.0\m\general\num2str 中的一个函数。米
>> which('cellstr')
'cellstr' 是来自文件 libinterp/octave-value/ov-cell.cc 的内置函数
which('int2str')
>> which('int2str')
'int2str' 是一个变量`
-
由于某种未知原因,我只测试了其他函数,而不是“int2str”,因此我在使用阴影函数时偶然发现了错误:
>> strcat('x',num2cell(int2str(1:10)))
error: int2str(10): out of bound 3 (dimensions are 1x3)
(注意:变量 'int2str' 阴影函数)
-
无论出于何种原因,我不小心通过分配
int2str = [1:3]
隐藏了该函数,导致了奇怪的行为。