Matlab Function Problem - Variable Storage

5 views (last 30 days)
Evan on 19 Oct 2014
Answered: Iain on 20 Oct 2014
Hi, I'm currently doing an assignment with Matlab functions and am having some trouble getting started. When I run this script, it does not return an error, but the 13x2 matrix loaded from the file is not saved as x. Instead, it seems to be stored as "ans". I need to use this variable to find the mean value and standard deviation in another function (with both functions being called in a "main" file). However, it always returns the error that the variable "x" is undefined. This problem persists regardless of whether I type, for example, "mean_value = mean(x)" or "mean_value = mean(ans)", leading me to believe that no variable is being stored at all.
The code is as follows:
function [x] = load_data
load_data = input('Which data file would you like to analyze? ', 's');
while exist(load_data) ~= 2
load_data = input('Please input a valid file name. ', 's');
[x] = load(load_data);
And the code for the analysis of the data is:
function [mean_value,std_dev] = analysis(data)
mean_value = mean(data);
std_dev = std(data);
disp(['The mean of the data is ', num2str(mean_value)]);
disp(['The standard deviation of the data is ', num2str(std_dev)]);
Like I said, both of these functions are being called upon in a separate file, which looks like this:
add_header ((not relevant to problem))
I also get the error "Not enough input arguments" depending on how I fiddle around with the code. The solution is probably something basic I'm just not getting, but any help would be appreciated.
  1 Comment
Evan on 19 Oct 2014
Oops! I copied the code wrong. Instances of "x" in the first function should read "data". I've been messing with the code to see if I could get different/better results. My apologies.

Sign in to comment.

Answers (2)

Orion on 20 Oct 2014
your functions load_data and analysis seem quite OK.
the problem is how you call them.
load_data is a function with no input and one output.
analysis is a function with one input and two outputs.
so instead of
add_header ((not relevant to problem))
you just need to do
add_header ((not relevant to problem))
mydata = load_data;
[mymean,mystd] = analysis(mydata);
when you called load_data without defining an output, the defaut output variable name ans is used by matlab to store the result of _the function.

Iain on 20 Oct 2014
Ok, a FUNCTION is a thing like "sin", it accepts a (none/list of) input parameter(s). It outputs a (none/list of) output parameter(s) and effects (like writing/moving files).
A SCRIPT is a list of commands that get executed in the current workspace. It doesn't need any inputs, it simply uses the available variables within the workspace, and EVERYTHING it does is visible to the workspace.
The separate file appears to be a script. load_data and analyse are functions - You need to tell them what their inputs are, explicitly, and you need to accept their outputs, explicitly into specific variables, or they'll (sometimes) just use "ans".


Find more on Files and Folders 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!