How to create a table from strings and numerical data?

38 views (last 30 days)
I have a loop and I don't know how many times it runs. Through this loop I get a string from my data and a vector with let's say 10 numbers.
what I'm trying to do is to create an empty table that after each run of loop adds string and the vector like this:
for i=1:length(endLoop)
str = get_my_string; % this returns a string that I was talking about earlier,let's say 'hello'
vec = get_my_vector % this gets me the vector mentioned earlier, let's say [1 2 3 4 5 6 7 8 9 10]
Now I need to concatenate my string and vector to get something like this:
[str vec] like ['hello' 1 2 3 4 5 6 7 8 9 10];
Then I need to write this into a table as a row. How can I do that?
Also I don't know the number of n or m, and I don't want to preallocate anything as the sizes are unknown. I wanna start with an empty table and then after first iteration I write the first line of the table, second iteration second line is written, and so on.
Thank you all for your help.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Sep 2022
Edited: Walter Roberson on 26 Sep 2022
Provided that you already initialized YourTable with variable type string for the first variable and cell for the second variable,
YourTable(end+1,:) = {str {vec}};
  2 Comments
Walter Roberson
Walter Roberson on 26 Sep 2022
For example,
YourTable = table('Size', [0 2], 'VariableNames', {'str', 'vec'}, 'VariableTypes', {'cellstr', 'cell'});
Letters = 'a':'z';
for K = 1 : 7
str = Letters(randi(26, 1, 5));
vec = randi(10, 1, 4);
YourTable(end+1,:) = {str, {vec}};
end
YourTable
YourTable = 7×2 table
str vec _________ _____________ {'ucvqj'} {[ 1 9 6 4]} {'ugrft'} {[ 4 1 5 6]} {'rjuoy'} {[ 5 1 6 8]} {'ylkpo'} {[10 8 10 9]} {'qqmwx'} {[ 2 6 3 8]} {'nwlzh'} {[ 6 4 2 3]} {'pqdwu'} {[ 5 3 3 3]}
Walter Roberson
Walter Roberson on 26 Sep 2022
This assumes that you want a variable that is the entire vector of numbers, rather than wanting individual variables for each of the numbers in the vector.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 26 Sep 2022
" I don't want to preallocate anything as the sizes are unknown. I wanna start with an empty table and then after first iteration I write the first line of the table, second iteration second line is written, and so on."
So lets take the two things we do know: the text and the numeric vector, and place each of them into one column of the table:
T = table('Size',[0,2], 'VariableTypes',{'string','cell'}, 'VariableNames',{'txt','vec'})
T = 0×2 empty table
W = warning('off','MATLAB:table:RowsAddedExistingVars');
for k = 1:randi(9)
S = string(char(randi([65,90],1,5))); % random text
V = randi(9,1,randi(9)); % random numeric vector of random size
T.txt(k) = S;
T.vec{k} = V;
end
warning(W)
T
T = 7×2 table
txt vec _______ ___________________ "NUDBP" {[ 9 1 7 6 4 2 7]} "ULVLA" {[ 6 2 3]} "MJHNE" {[6 1 2 6 7 5 1 6]} "FAXND" {[ 1 9]} "IRTRR" {[ 1]} "HRUQB" {[ 3 8 2]} "RAOZX" {[ 1 1 5 3]}

Products

Community Treasure Hunt

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

Start Hunting!