Variable Definition in terms of the function variable

3 views (last 30 days)
Dear all,
I just came across variable definitions in matlab script within a function by calling the output variable of the function,example
function Add= sum(x,y)
Add.max=50;
end
then ,why do we define variable max likwise?
Thanks

Answers (1)

Walter Roberson
Walter Roberson on 11 Apr 2020
Edited: Walter Roberson on 11 Apr 2020
That does not define a variable named max. Instead it creates the output variable as a struct with a field named max.
  3 Comments
Walter Roberson
Walter Roberson on 11 Apr 2020
Although it is not common for a function to output a struct that has only one field, it does happen sometimes.
For example you might have a generic loop framework,
%driver
fun = initialize_function();
state = initialize_state();
iteration_count = 0;
while ~isempty(state)
state = fun(state);
iteration_count = iteration_count + 1;
end
fprintf('\nThat took %d iterations\n', iteration_count);
This general framework does not need to know anything about what the function is: it just has to know that there is a state structure that gets passed in and an updated state structure gets passed out.
In something like this, you need to distinguish between the cased where the iteration function is saying that there is nothing left to do (state returned as empty), compared to the situation where the function just happens to use a single variable that is currently empty, so initialize_state might be returning a struct with a single field that just happens to be empty because that means something to the function.
function state = initialize_state();
state.max = [];
end
function fun = initialize_function();
if rand() > 0.5
fun = @bottles;
else
fun = @readfile
end
end
function state = bottles(state)
if isempty(state.max)
state.max = 99;
end
if state.max <= 0
fprintf('Time to restock!\n');
state = [];
end
fprintf('%d Bottles of beer on the wall!\n', state.max);
fprintf('%d Bottles of beer!\n', state.max);
fprintf('You take one down, and pass it around;\n');
state.max = state.max - 1;
fprintf('%d Bottles of beer on the wall.\n\n', state.max);
end
function state = readfile(state)
if isempty(state.max)
state.max = fopen('data.txt', 'r');
if state.max <= 0
fprintf('File fail\n');
state = [];
end
end
if feof(state.max)
fclose(state.max);
fprintf('Done with file\n');
state = [];
end
L = fgetl(state.max);
fprintf('Input line was: "%s"\n', L);
end
You can see that the section marked driver does not have to know anything about what the iteration routine is for, or anything about what the state variable means, other than that empty signals end of loop. You can also see from the way I assign the handle randomly that fundamental aspects of the processing can depend upon dynamic needs or upon user input, and yet the same generic framework still applies. Also clearly more than one field could have been put into the state structure, with it still being the case that sometimes it would be appropriate to only define one field inside a function.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!