MATLAB Answers

Matlab Function Problem - Variable Storage

조회 수: 6(최근 30일)
Evan
Evan 19 Oct 2014
답변: Iain 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');
end
[x] = load(load_data);
end
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)]);
end
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))
load_data
analysis
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

Evan
Evan 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.

답변(2개)

Orion
Orion 20 Oct 2014
Hi,
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))
load_data
analysis
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.

  댓글 수: 0

Sign in to comment.


Iain
Iain 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".

  댓글 수: 0

Sign in to comment.


Translated by