Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

Wrong output and with a special sign "[square]" although same code works in "Octave-online"

Octave 6.1.0 (GUI)

This is a spin-off from Octave: How to turn a vector of integers into a cell array of strings?.

>> a = 1:3;
>> cellstr(int2str(a(:)))
ans =
{
  [1,1] = "[square]" 
}

enter image description here

While the output should be:

ans =
{
  [1,1] = 1
  [2,1] = 2
  [3,1] = 3
}

octave-online.net:

enter image description here

How to fix this?

Only for your information and not as an answer, a workaround without this issue would be cellstr(num2str(a(:))).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is caused by a terrible beginner's mistake.

The first helping comment under the question has already shown the way.

  1. If the online Octave is totally different from your GUI, it is probably your fault.

  2. In order to find the problem, check the functions that might cause the difference:

    >> which('num2str')

    'num2str' is a function from the file C:UsersUSERNAMEAppDataLocalProgramsGNU OctaveOctave-6.1.0mingw64shareoctave6.1.0mgeneral um2str.m

    >> which('cellstr')

    'cellstr' is a built-in function from the file libinterp/octave-value/ov-cell.cc

    which('int2str')

    >> which('int2str')

    'int2str' is a variable`

  3. Since I have for some unknown reason tested only other functions, but not the 'int2str', I accidentally found out about the error when using the shadowed function instead:

    >> strcat('x', num2cell(int2str(1:10)))

    error: int2str(10): out of bound 3 (dimensions are 1x3)

    (note: variable 'int2str' shadows function)

  4. For whatever reason, I had accidentally shadowed the function by assigning int2str = [1:3], leading to the strange behaviour.

Arbeitsumgebung = work environment: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...