How to make vector of strings into column headers of table

I have a vector of dates:
a =
Oct_2014
Nov_2014
Dec_2014
and I want to use the array2table function to make a table with these dates as the column headers:
array2table([10 12 14],'VariableNames',a).
It will work if a = {'Oct_2014' 'Nov_2014' 'Dec_2014'} but does not as given above. How do I go from the first instance of 'a' to the second?

Réponses (1)

a = {'Oct_2014' ;'Nov_2014' ; 'Dec_2014'}
b=array2table([10 12 14],'VariableNames',a)

5 commentaires

The output of a function I run gives a =
Oct_2014
Nov_2014
Dec_2014
I am not sure how to "insert" this vector 'a' into array2table([10 12 14],'VariableNames',a) without it returning an error. I know if 'a' were in the form {'Oct_2014' 'Nov_2014' 'Dec_2014'} it would work, but I do not know to transform the first 'a' into this second, or into any other way that would allow the array2table function to work. I do not want to manually type in the dates into an array.
What is a? a char array? a cell array of string? something else?
What does
whos a
return?
'a' is just the name given the vector. 'a' could be given any other name
Of course, a is the name of the variable. We need to know its type, as it's not obvious from your statements. It could be a char array, a cell array of string, or something else.
whos a
will tell us for sure.
If, in matlab, you write
a =
Oct_2014
Nov_2014
Dec_2014
you'll get a syntax error. So, from your statement, we don't know what a is
Thank you. I was able to figure this out as 'a' was a char vector and I needed it as a string vector so I used the function cellstr.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Tags

Question posée :

le 25 Oct 2014

Commenté :

le 25 Oct 2014

Community Treasure Hunt

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

Start Hunting!

Translated by