phujeb
phujeb

Reputation: 1

Reshape MxN cell array to 1x(M*N) cell array MATLAB

I am trying to make a cell array for legend titles in a plot. The plot has repetitions of an experiment with different frequencies and 3 different sensors. I need a different colour for each different sensor and frequency. Hence in the following example with two frequencies I end up with a 3x2 cell array where I need the array in one dimension to use as a legend (6x1) but I can't figure how to achieve this.

titles = {'Radial Sensor';'Axial Sensor';'Azimuthal Sensor'};

for ii=1:3 
    for jj = 1:num_freq
        legtitles{ii,jj} = [titles{ii},' ',num2str(freq(jj)),' Hz'];
    end
end

Ans:

'Radial Sensor 15 Hz'       'Radial Sensor 60 Hz'
'Axial Sensor 15 Hz'        'Axial Sensor 60 Hz'
'Azimuthal Sensor 15 Hz'    'Azimuthal Sensor 60 Hz'

So, I need to take the second and third rows and concatenate them onto the end of the first.

Thanks!

Upvotes: 0

Views: 690

Answers (2)

knedlsepp
knedlsepp

Reputation: 6084

You could either reshape your final legtitles:

legtitles = reshape(legtitles',1,[]);

or rewrite your code to:

legtitles = {}
for ...
    for ...
        legtitles{end+1} = ...

Upvotes: 1

Luis Mendo
Luis Mendo

Reputation: 112759

To concatenate all rows into a single row: if you have

legtitles = {'Radial Sensor 15 Hz'       'Radial Sensor 60 Hz';
             'Axial Sensor 15 Hz'        'Axial Sensor 60 Hz';
             'Azimuthal Sensor 15 Hz'    'Azimuthal Sensor 60 Hz'};

just use

legtitles = legtitles.';
legtitles = legtitles(:).';

Upvotes: 0

Related Questions