简体   繁体   中英

construct a matrix with empty string in octave and later fill it with uneven number of characters in each row

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM