0
votes

I'm plotting a number of variables, say x,y,z in Matlab (there are plenty of them in the real code...). Just to avoid any stupid mistakes, is there anything like in Stata where you can define a list of local variables and using loop to plot for each variable of the list? For example

 local varlist "x y z"
 local n: word count `varlist' 
 local i=1 
 while `i'<=`n' {
     local var: word `i' of `varlist'
     hist `var'
     local i=`i'+1
 }

Plotting in Stata seems implausible, as the data has multi-dimensions. Thanks for your thoughts!

1
The Stata code can be greatly simplified to foreach v of varlist x y z { hist `v' }. (Adjust to multiple lines, as comments don't allow block code). - Roberto Ferrer
foreach v in x y z will work too. - Nick Cox

1 Answers

0
votes

OK. I figured out how-to. The data type in Matlab that serves as varlist shall be cell arrays. The Matlab translation of the above Stata code is

varlist={x,y,z}
for i=1:length(varlist) 
  figure(i)
  temp=cell2mat(varlist(i)) %Transform cell array to matrix (vector)
  hist(temp) 
end