can not load mat file correctly

1 次查看(过去 30 天)
Sam
Sam 2012-7-31
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

回答(2 个)

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

Image Analyst
Image Analyst 2012-7-31
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

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by