Take a vector calculated in one m-file into another
Show older comments
In m-file1 I create a vector e which contains all the error values I require. In m-file2 I would like to run mfile1 and then use the vector e as I could in m-file1. I'm pretty sure it's possible but I have no idea how to do it.
Regards
Harry A real novice
Answers (2)
Andreas Goser
on 20 Jan 2012
1 vote
Simple: Both MATLAB files are scripts (not starting with FUNCTION). You variable e will be simply available in the other file.
A bit complicated, but fundamentally better: Use functions and your variable e would be used as a parameter.
TAB
on 20 Jan 2012
If your m-files are scripts
then all the variables will be on base workspace. You can use variable directly in any scrip.
If you m-files are function
You can return the e from m-file1 function into m-file2 function.
function mfile2
Err_Vect = mfile1();
....
....
end
---OR---
Save the e in base workspace using assignin() from m-file1 and read them using evalin() from m-file2.
---OR---
Save e in .mat file using save() from m-file1 and load in m-file2 using load()
1 Comment
David Young
on 20 Jan 2012
The first part of this reply is sensible advice, but I would not recommend the alternatives (after "OR").
You should avoid assignin and evalin unless (a) you are expert and understand the implications and (b) you have an overriding reason to use them, which is rare. See Loren Shure's recent blog (at http://blogs.mathworks.com/loren/ ) for some reasons behind this.
The save/load solution is likely to be very slow, and could leave your disk cluttered with .mat files.
Follow the link in Andreas Goser's answer, and learn one of the most important aspects of programming: how to write functions and pass all kinds of information between them.
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!