![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177084/image.png)
Creating a code based on a figure?
51 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146408/image.png)
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)
0 Kommentare
Antworten (2)
Mischa Kim
am 11 Dez. 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/177084/image.png)
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.
Image Analyst
am 28 Dez. 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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172660/image.jpeg)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Subplots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!