create a file to call a function and graph it in matlab

  1. First, create a new function, let's call it myFunction and save it in an .m file in your current working directory. This function should contain the code you want to execute and graph.
main.m
function myFunction(arg1, arg2)
  % Your code here
  % Example code to generate a sample graph
  x = linspace(0,10,100);
  y = sin(x);
  plot(x,y);
end
152 chars
8 lines
  1. Next, create a new script in your current working directory, let's call it main.m. In this script, you will call your myFunction and pass any necessary arguments. Then, you can save the resulting graph to a file.
main.m
% Main script
arg1 = % your value;
arg2 = % your value;
myFunction(arg1, arg2); % Call your function

% Save your plot to a file
saveas(gcf, 'plot.png'); % The gcf command gets the handle to the current figure
210 chars
8 lines
  1. Run your main.m script in MATLAB. This will execute your myFunction, generate the graph, and save it to a png file in your current working directory.

related categories

gistlibby LogSnag