Writing characters to empty matrix

Hi,
I am writing a loop to construct individual row names for n-rows. The row ids will be characters and not numbers. Additionally, I need to make a one dimensional (row) matrix to write the variable name to the respective row through each loop. If I was writing numbers to the variable, e.g. vardata = zeros(rownum),then it would be no problem. But I cannot figure out how to create an empty matrix to write characters to an empty matrix. Additionally, will it matter if the size of the row id changes? For example, the size of the row id for the first 10 iterations will be something like abc_x_1 then for the next 90 iterations abc_x_12, and for the last two iterations abc_x_123.
Thanks,
Eric

 Risposta accettata

Matt Fig
Matt Fig il 29 Set 2012
Modificato: Matt Fig il 29 Set 2012
You may want to use cell arrays.
for ii = 30:-1:1
T{ii} = sprintf('abc_x_%i',ii);
end
Now look:
T{25}
Then if you really need a character array, let MATLAB take care of the spacing problems for you:
char(T)

7 Commenti

Eric
Eric il 29 Set 2012
I see how your loop works, but I should have been a little more specific with how my for loop works.
Every 101 iterations 'x' changes to 'y' then after another 101 iterations to 'z'. Every 303 iterations 'abc' changes to 'kne' then to 'hip' and so forth. I could have 33 loops to get the 3333 row ids I need, but that is not an efficient way to do it. Do you have a suggestion on how to incorporate the changes the of the id name?
Thanks
cell arrays are still easiest for what you need.
prefixes = {'abc', 'kne', 'hip'};
infixes = {'x', 'y', 'z'};
for prid = 1 : length(prefixes)
for ifid = 1 : length(infixes)
for K = 1 : 101
T{K, ifd, prid} = sprintf('%s_%s_%i', prefixes{prid}, infixes{ifid}, K);
end
end
end
T = T(:); %change from 3D array to column vector
Matt Fig
Matt Fig il 29 Set 2012
Modificato: Matt Fig il 29 Set 2012
There are many ways to do this. Here is one. I would recommend you pre-allocate T if you are going to make it very large.
N = 3; % Limit of numbers, adjust as needed.
C = sprintf('%i',1:N);
cnt = 1;
for ii = {'kne','hip','arm'} % Fill out as needed.
for jj = 'xyz' % Here too.
for kk = C
T{cnt} = [ii{1},'_',jj,'_',kk];
cnt = cnt + 1;
end
end
end
char(T)
By playing around with the structure of this loop you should be able to get exactly what you need.
Eric
Eric il 29 Set 2012
You are the man. Thank you so much.
You're welcome!
Eric
Eric il 30 Set 2012
I have one more issue. I now have a 3333x1 matrix consisting of characters, and I want to append that to the first column of a 3333x3333 matrix. I will end up with a 3333x3334 matrix. However, I cannot make a zeros matrix and write my two matrices to it because of the characters in the 3333x1 matrix. I've tried to horizontally concatenate the two matrices since the rows are the same length. That doesn't work. Is there a way to write both characters and numbers to a matrix?
Thanks
There is no way to write both characters and numbers to a numeric or character array. Only cell arrays can hold both numbers and characters.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by