s= struct(\'Hello\',0,\'World\',0);
for i = 1: 5
s_vec(i) = s;
end
I have definied a struct in Matlab within a script. Now i want to implement a f
Although I think Romain's answer is better practice, you can modify parameters without passing them in and out of a function if you use Nested Functions.
However, I do not like to use them because in complicated large functions it can be quite confusing trying to follow where things are being set and modified.
That being said here is an example of using a nested function to do what you want.
function nestedTest()
%Define your struct
s= struct('Hello',0,'World',0);
for i = 1: 5
s_vec(i) = s;
end
disp('Pre-Nested Call')
disp(s_vec(1))
set_s(1, 'Hello' , 1);%Set the first element of s_vec without passing it in.
disp('Post-Nested Call')
disp(s_vec(1))
function set_s (number, prop , value)
% Nested can modify vars defined in parent
s_vec(number).(prop) = value;
end
end
Output:
Pre-Nested Call
Hello: 0
World: 0
Post-Nested Call
Hello: 1
World: 0