join char/cell to double matrix

3 views (last 30 days)
fede
fede on 21 Sep 2015
Edited: Image Analyst on 21 Sep 2015
I have
c=['IBM';'SPY';'IVV'];
celldata=cellstr(c);
and
price= hist_stock_data(celldata');
I want a matrix as the following:
IBM SPY IVV
price1 price2 price3

Answers (1)

Image Analyst
Image Analyst on 21 Sep 2015
How about constructing a cell array
for col = 1 : size(c, 1)
% For each row in c
% Extract row from character array and place into a cell.
ca{col, 1} = c(col, :); % String goes into first row of cell array.
% Stuff number into second row of this column:
ca{col, 2} = price(col);
end
Or you could use a table variable instead of a cell array.
  2 Comments
fede
fede on 21 Sep 2015
yes but the size of prices is 840,3, and not 1,3
Image Analyst
Image Analyst on 21 Sep 2015
Edited: Image Analyst on 21 Sep 2015
Looks like you forgot to mention that at first so there's no way I could have known. So just add a loop to add rows
for col = 1 : size(c, 1)
% For each row in c
% Extract row from character array and place into a cell.
ca{col, 1} = c(col, :); % String goes into first row of cell array.
% Stuff number into rows of this column:
for row = 1 : size(price, 1)
ca{col, row+1} = price(row, col);
end
end
Since you're not yet familiar with for loops and the size function (or else you would not have asked me), you should probably look up information on the for loop and the size function in the help documentation, since it's pretty basic yet crucial to know.
Actually, you'd better study up on cell arrays in the FAQ also: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F because they're far trickier than for loops.

Sign in to comment.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!