1

How to add character ',' or '+' in a matlab 2d array. I've tried the following way.

clc 
clear all
close all
min=0;
max=1052;
random_int = randi([min max],5,10)
% random_int=[515,586,942,742;353,588,916,436]
% load('Random_Int_x.mat')
% random_int
[m,n]=size(random_int);
for i=1:1:m
    allOneString = sprintf('%d,' , random_int(i,:));
    allOneString= allOneString(1:end-1)% strip final comma
    Str_1(i,:)=allOneString
%     allOneString= strjoin(arrayfun(@(x) num2str(x),random_int(i,:),'UniformOutput',false),',');
end
 Str_1

Example of Input / Matrix

random_int =
 2     9     7     7     9     8     2     5     7     5
 6     1     9     9     6     1     9     4     1     0
 5     0     8     8     5     6     9     0     4     6
 0     9     9     8     7     5     6     3     7     8
 8     4     2     0     5     5     1     8     2     6

Output:

Str_1 =
5×19 char array
'2,9,7,7,9,8,2,5,7,5'
'6,1,9,9,6,1,9,4,1,0'
'5,0,8,8,5,6,9,0,4,6'
'0,9,9,8,7,5,6,3,7,8'
'8,4,2,0,5,5,1,8,2,6'

This works properly with random number between 0-9.. However if I put input above 9 --> 10 .. then matlab throws matrix dimension error.

Subscripted assignment dimension mismatch.
Error in Number_with_String (line 14)
Str_1(i,:)=allOneString;

For Input above 9:

random_int =
76    96    88    23    26    25    92     5    61    86
87    69    32    36    86    39    46    21    55    69
42    26    56    69    55    97    91    78    76    41
74    74    24     3    46    52    29    70    88     4
 7    48    13    69    15    12    79    91    90    24

Expecting output:

'76,96,88,23,26,25,92,5,61,86'
'87,69,32,36,86,39,46,21,55,69' ... etc

Any suggestion to resolve this ..

8
  • 1
    Your code worked originally because it is expected that there is one number per matrix element, and so the total number of commas inserted per row is the same so this means that the number of characters per row is the same. As soon as you have a different amount of digits per row, the total number of characters per row is no longer consistent and you will thus get a dimension mismatch. How do you propose to handle the inconsistent lengths per row? A cell array? You can't store this as a character array anymore. Commented Sep 28, 2017 at 8:02
  • What @rayryeng said. Plus, look at the variable Str_1 and probably it contains the ascii values and not the numeric values. Not sure that what you wanted...? Commented Sep 28, 2017 at 8:14
  • @rayryeng, I just wanted to save the matrix into a variable. I saw the same error and don't know how to fix it. Commented Sep 28, 2017 at 8:17
  • @Adiel, Str_1 got ascii characters due to size mismatch. Please see the comments by --"rayryeng" and he explains the reasoning. While number of characters are same, ,matrix elements between 0-9 .. there are no such error and Str_1 has similar output as I mentioned in the question. Commented Sep 28, 2017 at 8:19
  • 1
    You can store the char data with Str_1{i} instead. Commented Sep 28, 2017 at 8:23

3 Answers 3

2

Here's a way:

random_int = randi([0 500],5,10); % example data
y = mat2cell(random_int, ones(1,size(random_int,1)), size(random_int,2)); % split into rows
y = cellfun(@(x) sprintf('%i,', x), y, 'UniformOutput', false); % strings with commas
y = cellfun(@(s) s(1:end-1), y, 'UniformOutput', false); % remove last comma from each

Example result:

>> y
y =
  5×1 cell array
    '74,281,294,376,124,203,211,170,242,334'
    '488,268,31,84,404,74,205,178,215,20'
    '120,242,390,37,113,199,140,375,395,469'
    '455,94,115,476,28,20,365,213,181,31'
    '130,62,138,421,261,105,114,226,398,90'
Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend you use string which shipped in 16b. You can convert the result to char or cellstr if you need.

>> min=0; max=1052;
>> random_int = randi([min max],5,10)

random_int =

        532         145         857         264         616         793         558         494         327         688
        736         157         256         648         578         400         820          12         556         725
        938         271         978         498         965         597         983         354         174         787
       1010         885         368         370         300          79         136         170         633         474
        576         267         207         874         797          56         598         836         276          88

>> str = join(string(random_int),',')

str = 

 5×1 string array

   "532,145,857,264,616,793,558,494,327,688"
   "736,157,256,648,578,400,820,12,556,725"
   "938,271,978,498,965,597,983,354,174,787"
   "1010,885,368,370,300,79,136,170,633,474"
   "576,267,207,874,797,56,598,836,276,88"

>> char(str)

ans =

 5×39 char array

   '532,145,857,264,616,793,558,494,327,688'
   '736,157,256,648,578,400,820,12,556,725 '
   '938,271,978,498,965,597,983,354,174,787'
   '1010,885,368,370,300,79,136,170,633,474'
   '576,267,207,874,797,56,598,836,276,88  '

Comments

0
clc 
clear all
close all
min=0;
max=1052;
random_int = randi([min max],200,10);
[m,n]=size(random_int);
for i=1:1:m
    allOneString = sprintf('%d,' , random_int(i,:));
    allOneString= allOneString(1:end-1);    % strip final comma
    Str_1{i}=allOneString;
end
Str_1=Str_1'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.