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 Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by