No pre-defined input in a function
1 view (last 30 days)
Show older comments
Good evening,
I'm about to do some experiments and afterwards I would like to do exactly the same data processing (graphs and such), so I figured a function would be in order. However, for convenience I would like the input of the function to be dynamic, if that is possible.
So if I input name of subject A, the function loads A's data and does calculations.
My current code is:
function experiment(subjectname)
subject.data = load(subjectname);
end
And I have a script, which does nothing at the moment but try to use the function:
experiment(subjectA)
When running the script, I get the following error: "Undefined variable "subjectA" or function "experiment""
Both the experiment.m and subjectA.mat are in my MATLAB directory.
0 Comments
Accepted Answer
Image Analyst
on 8 Apr 2013
You need to pass in the filename as a string.
baseFileName = 'subjectA.mat';
% folder might be pwd or whatever....
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file')
% File exists, use it
experiment(fullFileName);
else
% File doesn't exist - alert user.
errorMessage = sprintf('Warning: file not found:\n%s', fullFileName);
uiwait(warndlg(errorMessage));
end
5 Comments
Image Analyst
on 10 Apr 2013
Your function opens the mat file and reads the contents. What you're missing is to pass it back
function subject = experiment(subjectname)
or to load it into a global variable with setappdata() or the global keyword.
More Answers (0)
See Also
Categories
Find more on Environment and Settings 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!