How to access a string array and use the contents as titles within array2table

Meancoh is an 24x6 ordinary array.
meancoh=zeros(TT2,n1);
I've created a 6x1 string array called storehere, that, if accessed, contains the following:
"mean of bootstrapped coherence between X and Y"
"mean of bootstrapped coherence between X and Y"
"mean of bootstrapped coherence between X and Y"
"mean of bootstrapped coherence between X and Y"
"mean of bootstrapped coherence between X and Y"
"mean of bootstrapped coherence between X and Y"
Now, I think I ought to use array2table to assign the 6 rows above to each of the 6 columns of data in meancoh. I've tried but I don't seem to get it right.
Any suggestions?
P.S.
I asked a question relating to a previous problem (i.e. how to create storehere), which I cancelled once I realised I was just missing a bracket. I didn't realise someone had already commented-I saw an email to that effect just after, with a comment from 'the cyclist'. I apologise, as I didn't see someone had already made an effort to answer. The lines below work. I was missing a smooth bracket in the loop.
Now I have a string array called nameVar that contains this:
x y
x v
x z
y v
y z
v z
Fine. Now I need to create 6x1 string array that in which each row says ‘mean of bootstrapped coherence between (:) and (:).
This doesn’t work:
storehere=strings(Nrowscombinations,1);
for kkk=1:length(Nrowscombinations)
storehere(:)=strcat({'mean of bootstrapped coherence between '}, nameVar(kkk,1),{ ' and ' }, nameVar(kkk,2));
end

 Accepted Answer

I would create your storehere array, this way:
storehere = compose("mean of bootstrapped coherence between %s and %s", nchoosek(["X", "Y", "V", "Z"], 2))
However, there is absolutely no way that you can use these strings as variable names in a table. Table variable names, like all matlab variable names have some restrictions on which characters are allowed. In particular, spaces are not allowed.
You could use matlab.lang.makeValidName to make valid variable names out of your string array. It will remove the spaces, or you could replace the spaces by _:
array2table(meancoh, 'VariableNames', matlab.lang.makeValidName(storehere))
%or
array2table(meancoh, 'VariableNames', strrep(storehere, ' ', '_'))
However, I would recommend that you use shorter variable names altogether.

4 Comments

I agree with what Peter and Guillaume said. We can abbreviate the "mean of bootstrapped coherence" part of the information describing the purpose of those variables as meanBSC or something similar and use that as a prefix for the variable names in your table (meanBSC_X_V, meanBSC_Y_V, etc.)
Alternately, name the variable containing your table meanBoostrapCohere or something similar and have X_V, Y_V, etc. be the names of the variables in the table. Then retrieving those variables would be meanBoostrapCohere.X_V and that's pretty suggestive of the significance or purpose of the data stored in the variable.
Amazing. Thank you all. One last question. I’ve done a pretty ugly section of code that requires quite a bit of manual work, which is innocuous with 4 columns of random data, but would be a pain once I roll out the program to real data, of which I have tons. But ugly was all I was able to conjure up.
So, this:
[nRows, nCols] = size( data ) ;
combinations =nchoosek(1:nCols,2);
gives me an array of unique combinations:
1 2
1 3
1 4
2 3
2 4
3 4
In order to associate each number in array combinations to the variable names, where these are contained in array ‘headers’, from:
headers = raw(1,1:4);
I’ve done this:
Nrowscombinations=size(combinations,1);
Ncolumnscombinations=size(combinations,2);
nameVar = strings(Nrowscombinations,Ncolumnscombinations);
for iii=1 : Nrowscombinations
for jjj =1 : Ncolumnscombinations
if combinations(iii,jjj)==1
nameVar(iii,jjj)=headers{1,1};
end
if combinations(iii,jjj)==2
nameVar(iii,jjj)=headers{1,2};
end
if combinations(iii,jjj)==3
nameVar(iii,jjj) = headers{1,3};
end
if combinations(iii,jjj)==4
nameVar(iii,jjj)=headers{1,4};
end
end
end
And nameVar is what I want, a 6x2 array of strings.
Is there an intelligent way of doing this, one that wouldn’t have me manually insert that co-ordinates and the numbers like that –you can imagine how hideous this becomes with hundreds of variables?
I believe I've shown exactly how to do it in my answer, with the nchoosek(["X", "Y", "V", "Z"], 2)).
It would be the same here:
nameVar = nchoosek(headers, 2); %all done
Note that even if you used your original code, that loop is completely pointless. You could have done:
combinations =nchoosek(1:size(data, 2), 2);
nameVar = headers(combinations);

Sign in to comment.

More Answers (1)

It's not really clear what you are doing and what went wrong. If your matrix of data has 6 columns, then array2table will accept a 6-element string array or cell array of char rows as the variable names. And you'r problem is how to create those names?
For one thing, the names have to be valid MATLAB identifiers, and unique. I'm gonna suggest that you name the variables "x_y", "x_v", etc., and set the VariableDescriptions property of the table to "mean of bootstrapped coherence between x and y", etc. I imagine something like this
>> namevar = ["x" "y"; "x" "z"; "y" "z"]
namevar =
3×2 string array
"x" "y"
"x" "z"
"y" "z"
>> "mean of bootstrapped coherence between " + namevar(:,1) + " and " + namevar(:,2)
ans =
3×1 string array
"mean of bootstrapped coherence between x and y"
"mean of bootstrapped coherence between x and z"
"mean of bootstrapped coherence between y and z"
is what you need.

1 Comment

I don't know how I didn't see that the way I wrote the loop to fill up 'storehere' was wrong.
Based on what you said above, this:
for kkk=1:length(Nrowscombinations)
storehere(:)="mean of bootstrapped coherence between " + nameVar(:,1) + " and " + nameVar(:,2);
end
does the trick. storehere is a 6x1 string array:
"mean of bootstrapped coherence between X and Y"
"mean of bootstrapped coherence between X and V"
"mean of bootstrapped coherence between X and Z"
"mean of bootstrapped coherence between Y and V"
"mean of bootstrapped coherence between Y and Z"
"mean of bootstrapped coherence between V and Z"
My question is how do I tell array2table to accept the strings in storehere as the valid variable names?

Sign in to comment.

Categories

Products

Community Treasure Hunt

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

Start Hunting!