tags:

views:

99

answers:

1

Hi, I want to plot several curves, each having a different length. So, I've placed each curve as an array in a cell index, Y (this allows me to index through arrays of different sizes inside a FOR loop). I use "hold all" below to enable each iteration of the FOR loop to plot each new array in the cell array Y inside the same plot.

hold all;
for i = 1:1:length(maxusers)
  time = increment*(0:1:length(Y{i})-1);
  plot(time,Y{i,1,:});
end

While the use of a cell array here does simplify plotting the various curves inside Y, the problem I'm having is creating legend. Currently I'm using a really long/ugly switch statement to cover every possible scenario, but I think there should be a more elegant solution.

If I have an array (where maxusers=4, for example) that is:

 filesize = [10 100 200 300];

I know the legend Matlab command that works is:

legend(num2str(filesize(1)),num2str(filesize(2)),num2str(filesize(3)),num2str(filesize(4)));

but I get stuck trying to create a legend command when the number of curves is a variable given by maxusers. Any ideas? Thanks in advance.

+3  A: 

Try this:

>> filesize = [10 100 200 300];
>> str = strtrim(cellstr(int2str(filesize.')))  %'# Create a cell array of
                                                %#    strings
str = 

    '10'
    '100'
    '200'
    '300'

>> legend(str{:});  %# Pass the cell array contents to legend
                    %#   as a comma-separated list
gnovice
thanks so much gnovice! I can eliminate about 100 lines of my 'switch' code with this simple statement.
ggkmath