can not load mat file correctly

1 view (last 30 days)
Sam
Sam on 31 Jul 2012
How to make the function load_dataset below return x value of 2 ?
Thanks
S
>>clear all;
>>x=2;y=4;
>>save('test.mat');
>>x = load_dataset('test.mat','x');
>>x
x =
x
----
function dataset = load_dataset(cfmat,dataset)
try
load(cfmat,deblank(dataset));
catch
dataset=[];
end

Answers (2)

Ilham Hardy
Ilham Hardy on 31 Jul 2012
Why not use load('test.mat','x')

Image Analyst
Image Analyst on 31 Jul 2012
Try something like this (untested):
function storedDataset = load_dataset(cfmat, dataset)
try
storedStructure = load(cfmat);
% Check to see if the requested field exists in the structure.
tf = isfield(storedStructure, dataset)
if tf
% The field exists. Use dynamic fieldname to extract it.
storedDataset = storedStructure.(dataset);
else
storedDataset = [];
end
catch ME
warningMessage = sprintf('%s\n', ME.message);
fprintf('%s\n', warningMessage);
uiwait(warndlg(warningMessage));
storedDataset =[];
end

Community Treasure Hunt

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

Start Hunting!