Function returns ans rather than actual output variables
14 views (last 30 days)
Show older comments
Hi,
I am trying to write a function (I must use a function) that accepts a string from the user and returns 3 output values, which I would like to save in the general workspace for future use (in a different function). However, the function returns only an ans variable - which is equal to the value of the first output value requested. I searched quite extensively for a solution, as this seems like a common issue for beginners, however, I couldn't find anything that works for me (or even explains the problem). The code is written below, thanks for your help.
if true
%atm the function would not save the output variables, but returns an ans
%variable instead
function [case_des_string,case_urgency, Case_urg] = HowUrgent (case_des_string);
%This function accepts a string from the user, describing the case
%properties, and returns the case urgency as a conversion of the
%alphabetical scale given by the user.
%The string provided by the user is in the following order: cat/dog,
%urgency level, gender, serial arrival number, and animal's age in
%years.The string is overall 8 characters length.
%asks for user input
case_des_string = input('Please insert your case details, in the following order:\nC/D for animal type, case urgency level (A-J), animal''s gender\n3 digits serial arrival number and 2 digits age\n For example: ca123456\n', 's');
%if statement to ensure the string is in the desired length
if length(case_des_string)~=8;
case_des_string = input('Please insert your case details, in the following order:\nC/D for animal type, case urgency level (A-J), animal''s gender\n3 digits serial arrival number and 2 digits age\n For example: ca123456\n', 's');
end
%case switch to convert urgency level from letter to number
case_urgency = case_des_string(2);
case_urgency = upper(case_urgency);
switch case_urgency;
case 'A'
Case_urg = 1;
disp('Urgency level is 1, please contact the clinic''s admission office');
case 'B'
Case_urg = 2;
disp('Urgency level is 2');
case 'C'
Case_urg = 3;
disp('Urgency level is 3');
case 'D'
Case_urg = 4;
disp('Urgency level is 4');
case 'E'
Case_urg = 5;
disp('Urgency level is 5');
case 'F'
Case_urg = 6;
disp('Urgency level is 6');
case 'G'
Case_urg = 7;
disp('Urgency level is 7');
case 'H'
Case_urg = 8;
disp('Urgency level is 8');
case 'I'
Case_urg = 9;
disp('Urgency level is 9');
case 'J'
Case_urg = 10;
disp('Urgency level is 10');
end
end
end
2 Comments
Dennis
on 7 May 2018
It works fine for me.
You need to tell Matlab where to store your return values when you call your function:
[string,urgency, case] = HowUrgent()
I'd like to point out that your function does expect an input, but you are not using it.
Stephen23
on 7 May 2018
Edited: Stephen23
on 7 May 2018
@aviv kadair: you didn't call the function with any output arguments, just as Dennis states above. Very basic MATLAB concepts, like how to call functions with output arguments, are explained in the introductory tutorials (which are highly recommended for all beginners):
Note that every function has its own independent workspace, and it is intentional that variables do not just magically jump from one workspace to another: code that has no control over where variables appear or can be changed is uncontrollable spaggetti code.
To get values form one workspace into another one you should pass them yourself, as input and output arguments, just as the MATLAB documentation recommends:
Do NOT use global variables, or assignin, etc.
Answers (1)
John D'Errico
on 7 May 2018
Edited: John D'Errico
on 7 May 2018
When you call a function, ANY function, what happens? How do you use it?
For example, suppose I use the function sum? What do I get?
X = 1:5;
sum(X)
ans =
15
So if I give MATLAB no place to store the result, what does it do? It dumps it into the variable ans, displaying that into the command window as ans.
If I wanted to save the result as some variable, I can give it any name I want, right? So, i could do this?
sumX = sum(X)
sumX =
15
That stores the result into the variable sumX.
whos
Name Size Bytes Class Attributes
X 1x5 40 double
ans 1x1 8 double
sumX 1x1 8 double
So we see there are three variables defined in my current workspace, X, ans, and sumX.
Now, look at the help for sum?
help sum
sum Sum of elements.
S = sum(X) is the sum of the elements of the vector X. If X is a matrix,
S is a row vector with the sum over each column. For N-D arrays,
sum(X) operates along the first non-singleton dimension.
If we were to look at how the sum function is defined, it would look something like this:
function S = sum(X,DIM)
% sum Sum of elements.
...
with lots more stuff in it. Of course, sum is actually a compiled function, so we cannot look at the code itself, but that is essentially how it would be defined. So S is the output variable, and DIM is an optional input.
Note that when we just ran the sum function, why did it not return a variable named S?
The answer is because functions don't work that way! They return the result into whatever variable you put it in, OR it returns the result as a variable named ans, if you provide no place to put it. If there are multiple outputs, and you only give one result or none at all, then it dumps the all the outputs after the first output into the bit bucket.
So you have the function defined as:
function [case_des_string,case_urgency, Case_urg] = HowUrgent (case_des_string);
...
Now, suppose you run it as
HowUrgent (case_des_string)
what should it return, based on my explanation above?
Hint: (ans)
The first output gets put into ans, and all the others got dumped into the bit bucket, never to be seen. What they do NOT do is get put into variables named like you want them. Why not? BECAUSE FUNCTIONS DON'T WORK THAT WAY. All functions work the same way, just as I showed how sum responds. Just because you wrote it, your function is still just a normal function.
If you want it to put the results into something to store besides ans, how do you need to call it?
[case_des_string,case_urgency,Case_urg] = HowUrgent (case_des_string);
All of this would have been covered in the getting started tutorials for MATLAB. It is a good thing to read when you are learning the language.
2 Comments
Stephen23
on 7 May 2018
Guys, I read MATLAB help and documents over and over as well as my study notes and other notes available online, please don't assume I didn't do my research. I'm sorry, but I still don't understand any of what you wanted. I know the function workspace and the general workspace are not the same or even shared, this is why I said that I'd like to save specific variables in the general workspace. I don't understand what do you mean by "it returns the result as a variable named ans, if you provide no place to put it.". As far as I understand, by providing 3 variables in the brackets just after the word function - isn't that suppose to be the place where I reserve the space for my output?
If I call the function this way:
if true
[a,b,c] = HowUrgent
end
Then I get the actual values assigned to the variables I stated whilst calling the function, meaning a,b,c. However, my tutor is supposed to check this homework - together with other functions that should use this function- and I cannot predict the names he'd use when calling the function, so how can I use this information further along? Thanks
Stephen23
on 7 May 2018
Edited: Stephen23
on 7 May 2018
"my tutor is supposed to check this homework - together with other functions that should use this function- and I cannot predict the names he'd use when calling the function, so how can I use this information further along"
Simple: write a script that calls all of the functions, provide it to them along with the required functions, and tell them to run the script.
Magically accessing variable names, or magically making variables appear and disappear from different workspace would be horribly complex solution, and you should avoid doing this.
See Also
Categories
Find more on Scope Variables and Generate Names 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!