1

I have a working piece of code in MATLAB

empty_string = "";
bag = repmat(empty_string, 4, 1);
bag(2) = "second row";
bag(3) = "third";

However, I want to convert this to octave as MATLAB is a licensed version and everyone can't get access to it.

empty_string = blanks(1);
bag = repmat(empty_string, 4, 1);
bag(2) = "second row";
bag(3) = "third";

This gives error: =: nonconformant arguments (op1 is 1x1, op2 is 1x10)

I want each row of matrix 'bag' to be filled with uneven number of characters, please suggest how this can be done in octave. Thanks.

scoobydoo
  • 121
  • 8

1 Answers1

2

Since MATLAB strings have not been implemented in Octave, you will need to use a cell array of char arrays. Fortunately, this is pretty simple. Just change the first two lines in your code sample to create a cell array of the proper size:

bag = cell(4,1);
bag(2) = "second row";
bag(3) = "third";

The result is:

bag =
{
  [1,1] = [](0x0)
  [2,1] = second row
  [3,1] = third
  [4,1] = [](0x0)
}

The one annoyance is that in order to reference your char arrays in the cell array, you need to use curly braces instead of parentheses:

>> second = bag{2}
second = second row
beaker
  • 16,331
  • 3
  • 32
  • 49
  • Well, I have used cell arrays but it is not convenient. I need to manually reference each cell array with paranthesis{} in entire code. But if I don't have any other solution, I have to proceed with this. Thanks. – scoobydoo Apr 11 '22 at 14:18