Filter löschen
Filter löschen

Load M-file via function in workspace

8 Ansichten (letzte 30 Tage)
Herbert Walter
Herbert Walter am 20 Jan. 2011
As I have many .mat files with measure data I'd like to write a function wich loads the chosen file, and plots the values from chosen columns in the matrix.
I tried:
% creates a function called 'measuredata'
function measuredata(nameoffile)
load(nameoffile)
end
but if I type in the command window, and want to load e.g. measurement102.mat:
>> measuredata('measurement102')
I get no warning but nothing is being loaded to the workspace. What can I do?
  2 Kommentare
Todd Flanagan
Todd Flanagan am 20 Jan. 2011
Herbert, I moved your response to a comment in Matt's answer.
Todd Flanagan
Todd Flanagan am 20 Jan. 2011
Also, it's a good practice to accept an answer if it helped you.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Matt McDonnell
Matt McDonnell am 20 Jan. 2011
Each function defines its own workspace that is used to evaluate commands and store the results in local variables. This differs from scripts, which manipulate the base workspace directly.
The data is loaded into the workspace of the function measuredata, which then returns to the base workspace without returning any output.
To return the data to the base workspace use:
function data = measuredata(nameoffile)
data = load(nameoffile);
% Do some plotting and analysis...
end
  2 Kommentare
Todd Flanagan
Todd Flanagan am 20 Jan. 2011
Herbert says, "But then I just get the "ans" variable in the workspace.. How can I load excactly the measurement102.mat to the workspace?"
Todd Flanagan
Todd Flanagan am 20 Jan. 2011
Matt responds, "Call the function from the MATLAB command line as
data = measuredata('measurement102');
This will create a struct called data in the workspace. You can then access the values using data.x, data.y, or whatever the names of the values in the mat file were."

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 20 Jan. 2011
As the file might contain multiple variables, the easiest way to duplicate the effect of loading them into the workspace of the caller is to use
% creates a function called 'measuredata'
function measuredata(nameoffile)
evalin('caller', sprintf('load(''%s'')',nameoffile))
end
This is not a good solution; using Matt's version and assigning the output to a variable and accessing the fields of that variable is much cleaner. The version I show here should only be used if it is important that random variables in the caller's workspace should be clobbered if they just happen to have the same name as a variable name saved in the file, or if it is important that Strange Things happen if a function used in the calling function has the same name as the name of a variable saved in a file. (And yes, some people do rely upon these kinds of oddities, unfortunately.)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by