Load variables from an m-file to use them in a function

5 views (last 30 days)
Hi, and thanks in advance. I have (many) parameters inside an m-file named data.m in the form:
item1.size=value1;
item1.weight=value2;
item2.size=value3;
item2.price=value4;
and so on and so on. Now I have a function of the form: [result]= analysis(item1,item2) and I want to load the file data.m because in order to perform the operations I need values like "item1.weight" and "item2.price". The 'load' command doesn't seem to work with this m-file and if I only type after the first line of my function file the name of my data file, it doesn't work either. It gives me the error "undefined function or variable item1". However, if I open and run the data.m file and then I execute my function, I get the result without problems. I just don't want to have to run the data.m file independently before running my function code. I don't know either how to save my data in a mat file. It is a huge data file and trying to save the variables individually is not an option. I tried changing the file extension from .m to .mat and then when using load I get an error as well.
I am not used to Matlab, so even if this is a simple issue, I would appreciate any help.
Thank you very much in advance,
Jose
  4 Comments
Adam
Adam on 6 Oct 2014
load
is used to load in .mat data files rather than .m code files so you would need to run your .m file and save the variables into a .mat file which you can then load in.
Jose
Jose on 7 Oct 2014
Thanks everyone for their answers, saving the .m file as a .mat with the suggestion of Oleg did the trick. I know it was a very simple issue but I am just getting used to Matlab. Thanks! :)

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!