Matlab: Can a large number of preallocations be placed in another file?

I am working on code in Matlab, where I have many variables that need to be pre-allocated (each variable has a length of 8760x1). Values ​​are generated in a for loop:

a=zeros(8760,1); b=zeros(8760,1); (...) for i=1:8760 a(i)=[some code]; b(i)=[some code]; (...) end 

However, seeing that I have many of these variables, I want to redirect the parameters to another file (cleaner).

preallocate.m

 a=zeros(8760,1); b=zeros(8760,1); ... 

main.m

 preallocate for i=1:8760 a(i)=[some code]; b(i)=[some code]; (...) end 

Will the preinstallation in another matlab file be as effective as in the same file as the executable? Other offers?

+1
source share
2 answers

Yes.

+4
source

This is a valid approach, but you must make sure that your variables fall into the workspace of your main function, i.e. You must install preallocate.m to:

 [a, b] = preallocate 

this way, when it is called in the main function, your initializations will be initialized.

0
source

Source: https://habr.com/ru/post/1215844/


All Articles