Clear Filters
Clear Filters

Creating a code based on a figure?

79 views (last 30 days)
BlahBlah
BlahBlah on 11 Dec 2014
Answered: Image Analyst on 28 Dec 2014
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)

Answers (2)

Mischa Kim
Mischa Kim on 11 Dec 2014
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 Comment
BlahBlah
BlahBlah on 11 Dec 2014
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.

Sign in to comment.


Image Analyst
Image Analyst on 28 Dec 2014
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)

Community Treasure Hunt

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

Start Hunting!