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

  • Thread starter Thread starter MatRobert
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
To export graphical data from a MATLAB .m file for further analysis, the recommended approach is to create a function that returns both the time and the cosine values. This can be achieved by defining a function at the beginning of the .m file, such as "function [x,time]=mygenfunc(T)", and saving it as "mygenfunc.m". This allows the user to call the function and obtain the time and x values, for example, using "[x,time]=mygenfunc(1)". Alternatively, if the graphical data is already plotted, the user can retrieve the data directly from the plot by accessing the properties of the plot's children, using commands like "c=get(gca,'Children'); time=get(c,'XData'); x=get(c,'YData');". This ensures that both the data and its corresponding domain are available for 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
Views
1K
Replies
2
Views
1K
Replies
8
Views
2K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
10
Views
3K
Replies
18
Views
6K
Back
Top