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

  • MATLAB
  • Thread starter MatRobert
  • Start date
  • Tags
    Matlab
In summary, the conversation is about exporting graphical data from a .m file to another .m file for the purpose of calculating the frequency. The best way to do this is to write a function that returns time and x and call it from another file. Alternatively, the code provided can be used to get time and x from the graph.
  • #1
MatRobert
4
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
  • #2
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:
  • #3


Hello,

Thank you for reaching out with your question. To export the cosine graph from your .m file, you can use the "save" function in Matlab. This function allows you to save variables from your workspace to a .mat file, which can then be loaded into another .m file for further analysis.

In your case, you can save the "time" and "x" variables as follows:

save('cosine_graph.mat', 'time', 'x');

This will create a .mat file named "cosine_graph" and save the variables "time" and "x" in it. You can then load this file into another .m file using the "load" function:

load('cosine_graph.mat');

Now, you can access the "time" and "x" variables in your new .m file and calculate the frequency as desired.

I hope this helps! Let me know if you have any further questions. Happy coding!
 

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

1. How do I export a cosine graph from a .m file in Matlab?

To export a cosine graph from a .m file in Matlab, you can use the "saveas" function. This function will save the current figure in the specified file format, such as .png or .jpg. Simply use the command "saveas(gcf, 'filename.format')" in your .m file to save the graph.

2. Can I customize the appearance of the exported cosine graph?

Yes, you can customize the appearance of the exported cosine graph by using various Matlab functions such as "plot," "xlabel," "ylabel," and "title." These functions allow you to change the color, line style, axis labels, and title of the graph.

3. How do I export multiple cosine graphs from a single .m file?

To export multiple cosine graphs from a single .m file, you can use the "subplot" function in Matlab. This function allows you to plot multiple graphs in a single figure window. You can then use the "saveas" function to export the entire figure as an image.

4. Can I export a cosine graph in a specific size or resolution?

Yes, you can specify the size and resolution of the exported cosine graph by using the "PaperPosition" property in Matlab. This property allows you to set the width, height, and resolution of the exported image. You can use the command "set(gcf, 'PaperPosition', [left, bottom, width, height])" to customize the size and resolution of the graph.

5. How do I export a cosine graph with a legend?

To export a cosine graph with a legend, you can use the "legend" function in Matlab. This function allows you to add a legend to your graph, which can include labels for different lines or curves. You can use the command "legend('label1', 'label2', ...)" to add a legend to your graph before exporting it using the "saveas" function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
201
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
781
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top