Run a script file "i" times and save output variable to a 3D array

2 vues (au cours des 30 derniers jours)
Kohen Bauer
Kohen Bauer le 13 Avr 2021
I have a script file "myscript.m" where each time the script file is executed I obtain a 1001x2 output variable "output".
I would like to execute this script "i" times in a loop, and save the output variable as an array (output,i) = (1001,2,i)
I've tried the following code in a new script file and am able to obtain a (1001,2,i) array, however, only the first column of my array is populated with the correct values from "output", the second column is zeros, which should not be the case.
result = zeros(1001,2,5); %preallocate array
for i = 1:5
run('myscript'); % where "output" = 1001x2, is computed
result(:,:,i)=[output(:,1),output(:,2)];
end
I know I've missed something trivial here.
  1 commentaire
DGM
DGM le 13 Avr 2021
Modifié(e) : DGM le 13 Avr 2021
Is there any reason why you're not using a function to do this? Just looking at this, there's no clear way to determine what's going on. I have a feeling that the entire problem is being caused by trying to run a script as a function and causing conflicts with variables left over from prior runs ... or something similar.

Connectez-vous pour commenter.

Réponse acceptée

Gargi Patil
Gargi Patil le 16 Avr 2021
The code provided by you should correctly assign the values to result. You can also simplify it by using the following code in the for loop:
result(:,:,i)=output;
The undesired assignment could be occurring due to incorrect assignment of values to output. Ensure that run() is computing output correctly.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by