Creating a code based on a figure?

조회 수: 28 (최근 30일)
BlahBlah
BlahBlah 2014년 12월 11일
답변: Image Analyst 2014년 12월 28일
I am trying to basically copy this graph for practice for my final coming up but I don't understand how to change the font,size or labeling the axis. Simply put, I need to replicate this graph exactly from my code. I need the font to be times new roman and size 18 with a marker size of 8.
This is my code:
clear
clc
x = linspace(0,2);
y1 = sin(2*pi*x);
y2 = exp(-0.5*2*pi*x).*sin(2*pi*x);
figure
subplot(2,1,1);
hPlot1 = plot(x,y1,'rs');
%// The important part.
set(gca,'YLim',[-1 2],'YTick',-1:1:2,'XTick',0:.5:2)
subplot(2,1,2);
hPlot2 = plot(x,y2,'k*');
set(gca,'YLim',[-0.2,0.6],'YTick',[-0.2,0,0.2,0.4,0.6],'XTick',0:.5:2)

답변 (2개)

Mischa Kim
Mischa Kim 2014년 12월 11일
Stavo, one thing you can do is run your code and then edit the plot manually by choosing "Show Plot Tools...", the symbol in the right corner of the plot window.
Once you have finished editing choose "Generate Code" in the "File" menu of the plot window. This will create the corresponding MATLAB code similar to yours but adapted to your requirements.
  댓글 수: 1
BlahBlah
BlahBlah 2014년 12월 11일
I tried that but for some reason it was not working properly plus it was giving me an answer with function but I think I have to avoid that and do everything manually which is why I was asking.

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


Image Analyst
Image Analyst 2014년 12월 28일
This gets you most of the way there.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
x = linspace(0,2, 100);
y1 = sin(2*pi*x);
y2 = exp(-0.5*2*pi*x).*sin(2*pi*x);
subplot(2,1,1);
plot(x,y1,'r-', 'LineWidth', 2); % Red line.
hold on;
plot(x(1:6:end),y1(1:6:end),'ks', 'MarkerSize', 10, 'LineWidth', 2); % Black squares.
ylim([-1,2]);
ylabel('f(t)', 'FontSize', fontSize);
text(.5, 1.2, 'Harmonic Force f(t) = sin(wt)', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
%// The important part.
set(gca,'YLim',[-1 2],'YTick',-1:1:2,'XTick',0:.5:2)
subplot(2,1,2);
plot(x, y2, 'k-', 'LineWidth', 2); % Black line
hold on;
plot(x(1:6:end),y2(1:6:end),'r*', 'MarkerSize', 10, 'LineWidth', 2); % Red Stars.
ylim([-.2,.6]);
ylabel('g(t)', 'FontSize', fontSize);
xlabel('Time (s)', 'FontSize', fontSize);
text(.45, .3, 'Forced Response g(t) = exp(-zetawt)*sin(wt)', 'FontSize', fontSize);
set(gca,'YLim',[-0.2,0.6],'YTick',[-0.2,0,0.2,0.4,0.6],'XTick',0:.5:2)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by