Create a String Array

I am trying to create a string array of elements ranging from A1 to H12, where the first 12 elements are A1 - H1, next 12 are A2 - H2 and so on. I want to do this without having to type these out in a comma separated list. Here is my failed attempt at programming this.
Rows = {'A','B','C','D','E','F','G','H'};
Cols = [1:1:12];
wellNames = NaN(1,96);
count = 1;
for y = Cols
ystr = sprintf('%d', y);
for x = Rows
tempstr = num2str(cell2mat([x, ystr]));
wellNames(:,count) = tempstr;
count = count + 1;
end
end

 Risposta accettata

Like this?
c = 'A':'E'
c = 'ABCDE'
s = string(c')
s = 5×1 string array
"A" "B" "C" "D" "E"
M = s + (1:6)
M = 5×6 string array
"A1" "A2" "A3" "A4" "A5" "A6" "B1" "B2" "B3" "B4" "B5" "B6" "C1" "C2" "C3" "C4" "C5" "C6" "D1" "D2" "D3" "D4" "D5" "D6" "E1" "E2" "E3" "E4" "E5" "E6"

4 Commenti

Anas Khan
Anas Khan il 5 Dic 2020
Thanks for your reply! Wow that’s really cool and creative! Nice! Actually, not quite though. I wanted to make it a row vector-kind of thing. So I can label my data matrix’s columns with it. I found some other questions of people advising to use the “table” function and wanted to make my label array first.
Anas Khan
Anas Khan il 5 Dic 2020
So literally going in a straight line from A1 to H1 then starting over from A2 to H2, until H12 is reached.
To change the shape of an array you can use reshape.
c = 'A':'E';
s = string(c');
M = s + (1:6);
rv = reshape(M, 1, [])
rv = 1×30 string array
"A1" "B1" "C1" "D1" "E1" "A2" "B2" "C2" "D2" "E2" "A3" "B3" "C3" "D3" "E3" "A4" "B4" "C4" "D4" "E4" "A5" "B5" "C5" "D5" "E5" "A6" "B6" "C6" "D6" "E6"
cv = reshape(M, [], 1)
cv = 30×1 string array
"A1" "B1" "C1" "D1" "E1" "A2" "B2" "C2" "D2" "E2" "A3" "B3" "C3" "D3" "E3" "A4" "B4" "C4" "D4" "E4" "A5" "B5" "C5" "D5" "E5" "A6" "B6" "C6" "D6" "E6"
Anas Khan
Anas Khan il 5 Dic 2020
Thanks!

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by