Variable Definition in terms of the function variable
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Walter Roberson
2020-4-11
编辑:Walter Roberson
2020-4-11
That does not define a variable named max. Instead it creates the output variable as a struct with a field named max.
3 个评论
Walter Roberson
2020-4-11
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!