Reputation: 587
I would want to install a few packages in an automated way and i was trying do this using a simple for loop. As shown below.
pkgs = '../pkgs';
names = dir(fullfile(pkgs, '*.tar.gz'));
n = numel(nomes);
for i = 1:n
pkg install names(n).name
end
This is first attempt that returns to me this 8 times
warning: file names(n).name does not exist
And i'm looking for a way to get the ans
value of names.name.
Upvotes: 1
Views: 58
Reputation: 13091
Instead of all that code, you can use 'glob()' to get the list of tarballs, and then a single pkg()
call to install all of them. Like so:
fpaths = glob ("pkgs/*.tar.gz");
pkg ("install", fpaths{:});
Upvotes: 3
Reputation: 35156
MATLAB and Octave both allow the easy syntax of
functionname arg1 arg2 ...
by translating it to the proper function call
functionname('arg1','arg2',...);
This implies that in order to pass the value of variables as arguments (rather than the variable names themselves), you must use the functional form:
pkgs = '../pkgs';
names = dir(fullfile(pkgs, '*.tar.gz'));
n = numel(names); %// fixed your typo here
for k = 1:n
pkg('install',names(k).name) %// changed here, also n -> i -> k
end
Note that you had two typos: names
was written as nomes
in line 3 (translation issue, probably), and more importantly, you were using n
rather than i
in the loop. As a matter of fact, don't use i
as a variable in Octave: that stands for the imaginary unit, and can lead to subtle errors if you're uncautious. I changed to k
in the above code.
Upvotes: 1