Filling an array with number and character in a for loop

Hi,
I want to fill an array with a loop with a character in the first column and in the second its number. Where sub_name is a 13 length character like 3873938_kdndnj
array_snr = zeros(length(SubIDs),2);
for sub_ind=1:10
array_snr(sub_ind, 1) = (sub_name);
array_snr(sub_ind, 2) = (snr);
end
But I cannot put the sub_name into the first column, since it is a character?
Best

3 Comments

Your concept fails for two reasons:
  1. numeric arrays can only hold numeric data.
  2. each element of a numeric array hold only one value (your example character vector has multiple elements).
You will have to rethink your algorithm, or use a container variable (such as a cell array). I would not recommend using a container variable as they make processing numeric data much more difficult.
Thanks alot for your answer. That is true. So would be a possible way to do two separate arrays one numeric and the other one with characters and concatenate them later on?
"So would be a possible way to do two separate arrays one numeric and the other one with characters and concatenate them later on?"
No. The entirety of any array is all the same class. You cannot concatenate different class arrays and keep their data as different classes.
It is possible to convert single characters to their character values and store these in a numeric array, but this is not likely to be useful for your situation.
Personally I would keep those two arrays separate. Both can be accessed by the same indexing.

Sign in to comment.

Answers (0)

Categories

Asked:

on 12 Sep 2018

Edited:

on 12 Sep 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!