many plots in object oriented way

조회 수: 16 (최근 30일)
Areum Lee
Areum Lee 2019년 1월 4일
답변: TADA 2019년 1월 4일
Hello,I am quite new using matlab and have one question regarding object oriented way for plots.
Currently, I have such a long code to have several plots with differenter x,y element in individual windows. Even tho it has different x,y elemennt , the overall features of each plot such as legend, x,ylimits, xylabels are pretty similar.
For exmaple, Currently I have more than 10 plots like below.
figure(1)
plot(seq(10:end),dr(10:end),'b');
hold on
plot(seq,ones(size(seq)) * 5,'r')
ylim([0 6])
xlim([0 20])
title('abcdb')
xlabel('Duration (s)')
ylabel('1st (-)')
legend('1','2')
grid on
hold off
print('-dmeta','figure1.emf')
figure(2)
plot(seq(10:end),dc(10:end),'b');
hold on
plot(seq,ones(size(seq)) * 5,'r')
ylim('auto')
title('abcdb2')
xlabel('Duartion (s)')
ylabel('2nd(-)')
legend('2','2')
ylim([0 6])
xlim([0 20])
grid on
hold off
print('-dmeta','figure2.emf')
I feel like that code is too long while it's just beautifying the graphs at the end.
that's why I thought of using classdef instead of writing down everything in the long long code.
Do you think it would work? or is there any other way to make customized function to shorten the entire code?
  댓글 수: 1
madhan ravi
madhan ravi 2019년 1월 4일
how about putting x and y in a matrix and just use a loop?

댓글을 달려면 로그인하십시오.

답변 (1개)

TADA
TADA 2019년 1월 4일
you can use a class, but I don't see how it will be better than a single function in your case
function plotMyData(figIndex, seq, dc, exportFileName, plotOpts)
if nargin < 4; exportFileName = ['figure' num2str(figIndex) '.emf']; end
if nargin < 5; plotOpts = struct; end
if ~isfield(plotOpts, 'title'); plotOpts.title = ['abcdb' num2str(figIndex)]; end
if ~isfield(plotOpts, 'startPlotAt'); plotOpts.startPlotAt = 10; end
if ~isfield(plotOpts, 'xlabel'); plotOpts.xlabel = 'Duartion (s)'; end
if ~isfield(plotOpts, 'ylabel'); plotOpts.ylabel = '2nd(-)'; end
if ~isfield(plotOpts, 'legend'); plotOpts.legend = {num2str(figIndex), num2str(figIndex)}; end
if ~isfield(plotOpts, 'ylim'); plotOpts.ylim = [0 6]; end
if ~isfield(plotOpts, 'xlim'); plotOpts.xlim = [0 20]; end
figure(figIndex);
plot(seq(plotOpts.startPlotAt:end),dc(plotOpts.startPlotAt:end),'b');
hold on;
plot(seq,ones(size(seq)) * 5,'r')
ylim('auto');
title(plotOpts.title);
xlabel(plotOpts.xlabel);
ylabel(plotOpts.ylabel);
legend(plotOpts.legend);
ylim(plotOpts.ylim);
xlim(plotOpts.xlim);
grid on;
hold off;
print('-dmeta',exportFileName);
end
and finally you can do what madhan ravi said and put loop through the data

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by