简体   繁体   中英

Defining Octave Functions

I am working on a school project with Octave for calculating and plotting velocity/acceleration graphs.

I have been trying to create a subplot function so that I won't have to hardcore it for every subplot as such

subplot(3, 1, 1);
plot(time, accn);
grid;
title('Acceleration vs Time')
xlabel('Time, (s)')
ylabel('Acceleration, (m/s^2)')

subplot(3, 1, 2);
plot(time, velocity);
grid;
title('Velocity vs Time');
xlabel('Time, (s)');
ylabel('Velocity, (m/s)');

Is it possible to create a function akin to this

subplot = subplotFunction(row, column, xaxis, yaxis, header, xaxisLabel, 
yaxisLabel)
subplot(3, row, column);
plot(xaxis, yaxis);
grid;
title('header')
xlabel('xaxisLabel')
ylabel('yaxisLabel')
endfunction

And then call it like this?

subplot = subplotFunction(1, 1, time, accn, 'Acceleration vs Time', 'Time, (s)', 'Acceleration, (m/s^2)')

I am quite new to using functions so my apologies :(

1;

function subplotFunction(row, column, idx, xaxis, yaxis, header, xaxisLabel, yaxisLabel)
  subplot (row, column, idx);
  plot (xaxis, yaxis);
  grid on;
  title (header)
  xlabel (xaxisLabel)
  ylabel (yaxisLabel)
endfunction

subplotFunction (3, 1, 1, 1:10, 11:20, "foo", "bar", "baz")
subplotFunction (3, 1, 2, 1:10, 11:20, "huhu", "haha", "hoho")
x = linspace (0, 10, 100);
subplotFunction (3, 1, 3, x, sin(x), "world", "boo", "doo")

print ("out.png")

gives

产量

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM