Matlab Q&A: Exporting Cosine Graph from .m File

  • Context: MATLAB 
  • Thread starter Thread starter MatRobert
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

This discussion focuses on exporting graphical data from a MATLAB .m file that plots a cosine graph. The recommended approach is to create a function named mygenfunc that returns both the time and cosine values. Users should define the function at the top of the .m file and save it as mygenfunc.m. Additionally, if users prefer to extract data directly from the plot, they can utilize the get function to retrieve the X and Y data from the current axes.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with function creation in MATLAB
  • Knowledge of plotting functions in MATLAB
  • Understanding of data extraction from graphical objects in MATLAB
NEXT STEPS
  • Learn about MATLAB function creation and return values
  • Explore MATLAB plotting functions and their properties
  • Research data extraction techniques from MATLAB graphical objects
  • Investigate frequency calculation methods in MATLAB
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly beginners and intermediate programmers, who are looking to export graphical data for further analysis or frequency calculations.

MatRobert
Messages
4
Reaction score
0
Hello,
I am new to Matlab and got a question to ask.

I have created a .m file which plots a cosine graph.
For simplicity, say
time=0:T/999:T;
x=cos(2*pi*time);
plot(time,x);

Then this will create a plot with cos(2piT) with domain of 0~T.

I will like to export this to other .m file so the file can read this graph and calculate the frequency of graph.
But I'm stuck as how I can export this graphical data to other .m file.

Because if I just lookup data on x, it will just give out an array of 1000 values and not knowing its domain(time), I can not calculate the frequency.

Hope this makes sense.

Will appreciate your helps!
 
Physics news on Phys.org
a couple of points:

Firstly the best way to do this is to write the function so that it returns time & x.

Put this at the very top of your m file
Code:
function [x,time]=mygenfunc(T)
Save the file as mygenfunc.m

Then call the function from somewhere using
Code:
[x,time]=mygenfunc(1);
for example.

If you are dead set on using the graph then the following code may be of use:
Code:
c=get(gca,'Children');
time=get(c,'XData');
x=get(c,'YData');
 
Last edited:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K