Variables not saved after function completes. Help needed
Show older comments
Hi all,
I have this function in checkfile.m
function [] = checkfile(filename, pathname)
if isequal(filename,0)
warndlg('File not selected.', 'Warning!', 'modal');
else
cd(pathname);
load(filename); % load variables
end
end
Inside this file, there are a number of variables. When this function completes, the variables are deleted. I do not want to save the variables as an array.
For example, inside file1.mat, there are 3 variables, a,b & c.
I want these 3 variables, a,b & c to be in the workspace as it is after the function exits.
How can I make the functions such that the variables do not delete itself after the function exits.
Accepted Answer
More Answers (2)
José-Luis
on 14 Oct 2012
Have the function return the variables you want to keep, otherwise they are destroyed upon exit.
function [a,b,c] = checkfile(filename,pathname)
%etc
Azzi Abdelmalek
on 14 Oct 2012
Edited: Azzi Abdelmalek
on 14 Oct 2012
In your function use
data=load(filename)
If filname.mat contains variables x y and z, you can get them, after caling your function
data= checkfile(filename,pathname)
by:
x=data.x
y=data.y
z=data.z
and don't forget to change, like mentioned by José luis
function data=checkfile(filename,pathname)
Categories
Find more on App Building in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!