Even when you just specify a single variable name, LOAD will still output it in a structure. In your case, one option you have is to use the function STRUCT2CELL to convert the output from LOAD into a cell array, then return this result using a variable output argument list:
function varargout = loadStructFromFile(fileName,environmentName)
varargout = struct2cell(load(fileName,environmentName));
end
Using VARARGOUT has the added benefit that, if environmentName
matches multiple variables in the .MAT file, you can return them all from your function. For example, if your .MAT file has three variable N1
, N2
, and N3
, and environmentName
is N*
, you can get them all by calling your functions with multiple outputs:
[N1,N2,N3] = loadStructFromFile('values.mat','N*');