EMChamp
EMChamp

Reputation: 449

MATLAB: Concatenate number value as string

I want to create a binary number in matlab and am having difficulty concatenating the numbers.

Here's what I tried so far:

testarray = zeros(10,10)
testarray = num2str(testarray) % Convert all values to type string

testarray(1,1) = num2str(1); % Fill with abitrary value

testarray(1,1) = strcat(testarray(1,1), num2str(0)); % Trying to make '10' here but instead I get this error: "Assignment has more non-singleton rhs dimensions than non-singleton subscripts"

Any help would be appreciated.

Upvotes: 0

Views: 997

Answers (1)

Oli
Oli

Reputation: 16035

In your example, the problem is that '10' has size [1,2], but testarray(1,1) has size [1,1]. So you might consider using cells instead:

testarray = cell(5,5);
testarray{1,1} = strcat(testarray(1,1), num2str(0)); 

By the way, you should have a look at the function dec2bin.

From the documentation:

dec2bin(23)
ans =
    10111

The resulting value is a string.

So if you want to concatenate two binary values (encoded as strings), just do:

['10' '11']
ans =
    1011

Upvotes: 1

Related Questions