2016-11-30 4 views
0
function res = plot2features(tset, f1, f2) 
% Plots tset samples on a 2-dimensional diagram 
% using features f1 and f2 
% tset - training set; the first column contains class label 
% f1 - index of the first feature (mapped to horizontal axis) 
% f2 - index of the second feature (mapped to vertical axis) 
% 
% res - matrix containing values of f1 and f2 features 

    % plotting parameters for different classes 
    % restriction to 8 classes seems reasonable 
    pattern(1,:) = 'ks'; 
    pattern(2,:) = 'rd'; 
    pattern(3,:) = 'mv'; 
    pattern(4,:) = 'b^'; 
    pattern(5,:) = 'gs'; 
    pattern(6,:) = 'md'; 
    pattern(7,:) = 'mv'; 
    pattern(8,:) = 'g^'; 

    res = tset(:, [f1, f2]); 

    % extraction of all unique labels used in tset 
    labels = unique(tset(:,1)); 

    % create diagram and switch to content preserving mode 
    figure; 
    hold on; 
    for i=1:size(labels,1) 
     idx = tset(:,1) == labels(i); 
     plot(res(idx,1), res(idx,2), pattern(i,:)); 
    end 
    hold off; 
end 

Я написал эту функцию, и я хочу показать несколько plot2featutes() в одной вдове в MATLAB.Как отображать несколько графиков в одном окне в Matlab?

Я попытался следующие,

subplot(2,2,1);plot2features(train, 2, 3); 
subplot(2,2,2);plot2features(train, 2, 3); 
subplot(2,2,3);plot2features(train, 2, 3); 
subplot(2,2,4);plot2features(train, 2, 3); 

Это не работает.

+1

Это не работает, потому что вы создаете новую фигуру в вашей 'функции plot2features' которая, * сюрприз сюрприз *, создает новую фигуру и построение делается в новый показатель – matlabgui

+2

Заметьте, создайте новую фигуру внутри 'plot2features'. Измените его, чтобы взять дескриптор оси как входной, и укажите это как аргумент '' Parent'' для команды plot. – mikkola

ответ

1

Как указано в комментариях, проблема связана с линией figure; в вашей функции. Без аргумента figure создает новую фигуру, а затем переключает фокус на эту фигуру. Ваша функция должна выглядеть следующим образом:

function res = plot2features(tset, f1, f2) 
% Plots tset samples on a 2-dimensional diagram 
% using features f1 and f2 
% tset - training set; the first column contains class label 
% f1 - index of the first feature (mapped to horizontal axis) 
% f2 - index of the second feature (mapped to vertical axis) 
% 
% res - matrix containing values of f1 and f2 features 

    % plotting parameters for different classes 
    % restriction to 8 classes seems reasonable 
    pattern = {'ks'; 'rd'; 'mv'; 'b^'; 'gs'; 'md'; 'mv'; 'g^'}; 

    res = tset(:, [f1, f2]); 

    % extraction of all unique labels used in tset 
    labels = unique(tset(:,1)); 

    % create diagram and switch to content preserving mode 
    for ii = 1:size(labels,1) 
     if ii == 1 
      hold off 
     else 
      hold on 
     end 
     idx = tset(:,1) == labels(ii); 
     plot(res(idx,1), res(idx,2), pattern{ii}); 
    end 
    hold off; 
end 

Я сделал три изменения в код:

  • я удалил figure и заменил его с тестом, который превращает hold off на первом проходе так, что новая ось и последующие графики затем рисуются внутри этих осей.
  • Я изменил свой индекс от встроенного i (который является SQRT (-1)) для ii, чтобы избежать неоднозначностей
  • Я изменил pattern в массив ячеек, без всякой причины, кроме моей личной эстетики.

вызовов, как это:

figure 
subplot(2,2,1);plot2features(train, 2, 3); 
subplot(2,2,2);plot2features(train, 2, 3); 
subplot(2,2,3);plot2features(train, 2, 3); 
subplot(2,2,4);plot2features(train, 2, 3); 
Смежные вопросы