Loading data in a function and passing it
Ältere Kommentare anzeigen
Hello all. I'm trying to write a function that checks for the existence of a series of variables. If they exist, I want the function to do nothing. If they don't exist, I want the function to load them into the workspace and pass them to the main workspace. When I run my code however, it loads the first file and assigns it to variable ans. Any help is appreciated.
function [cntREF, sREFn, w] = LoadClassData
%%%%Loads the preset data created by the training algorithm
if exist('B', 'var') == 1
return;
else
load('B.mat');
end
if exist('cntREF', 'var') == 1
return;
else
load('cntREF.mat');
end
if exist('sREFn', 'var') == 1
return;
else
load('sREFn.mat');
end
if exist('w', 'var') == 1
return;
else
load('w.mat');
end
end
1 Kommentar
Richard Prince
am 24 Feb. 2017
Akzeptierte Antwort
Weitere Antworten (1)
kowshik Thopalli
am 24 Feb. 2017
1 Stimme
In the else condition- write b=load('b.mat'), w=load('w.mat') etc. Does this solve your problem?
2 Kommentare
Richard Prince
am 24 Feb. 2017
kowshik Thopalli
am 24 Feb. 2017
Yes You are right. It becomes a structure.You have to then access using b.b; Yes. Functions have their own work space which is different from base workspace . Return the loaded matfiles to the script that calls this function.
Kategorien
Mehr zu Workspace Variables and MAT Files finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!