Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab Legend after FOR loop

I am creating a file to read in a certain number of .wav files, each of these corresponding to a musical note. I am performing an FFT on each of them and plotting them all on the same figure. However I am having a problem with getting the legend to print correctly, it is separating the names I want to use into individual letters instead of using them as a string. My code is as follows:

clear all
mydir = 'Note Values/';
wavFiles = dir([mydir '*.wav']);
length(wavFiles)

legendText = [];
figure(1);
hold on;
for i = 1:length(wavFiles)

    wavFiles(i).name
    [y, fs] = wavread([mydir wavFiles(i).name]);
    sound(y, fs)

    currentSample = y(round(length(y)/2)-2048:round(length(y)/2)+2047);

    FFT = abs(fft(currentSample));
    Power = FFT.*conj(FFT)/length(FFT);

    if (mod(i, 2) == 1)
        h = plot(Power, 'Color', 'red');

    else
        h = plot(Power, 'Color', 'blue');
    end

    sri = wavFiles(i).name;
    sri
    legendText = [legendText, sri];

end
length(legendText)
legendText(1)
legend(legendText(:));
hold off;

The sri variable is always a full string, but legendText(1) only prints out A instead of A3.wav. I know it's probably something really obvious but I just can't find it. Thanks

The output on my graph appears as this: alt text

like image 880
Joneser Avatar asked Oct 14 '22 17:10

Joneser


1 Answers

You should use

legendText{i} = sri

to fill the cell with strings and

legend(legendText{:});

at end.

like image 151
Clement J. Avatar answered Nov 02 '22 11:11

Clement J.