1

TL;DR: How should custom simulation runs be managed in Matlab? Detailed Questions at the end.

I am working with matlab where i created some code to check the influence of various parameters on a simulated system. It has a lot of inputs and outputs but a MWE would be:

function [number2x,next_letter] = test(number, letter) 
    number2x = number * 2;
    next_letter = letter + 1;
    disp(['next letter is ' next_letter])
    disp(['number times 2 is ' num2str(number2x)])
end

This works if this is all there is to test. However with time multiple new inputs and outputs had to be added. Also because of the growing number of paramters that have been test some sort of log had to be created:

xlswrite('testfile.xlsx',[num2str(number), letter,num2str(number2x),next_letter],'append');

Also because the calculation takes a few hours and should run over night multiple parameter sets had to be started at one point. This is easily done with [x1,y1] = test(1,'a');[x2,y2] = test(2,'b'); in one line or adding new tasks while the old still run. However this way you can't keep track on how many are still open.

So in total I need some sort of testing framework, that can keep up with changeging inpus and outputs, keeps track on already doen calculations and ideally also handles the open runs.

I feel like i can't be the only one who faces this issue, in fact I think so many people face this issue that Mathworks would already came up with a solution.

For Simulink this has been done in form of a Simluationmanager, but for Matlab functions the closest thing i found is the Testing framework (example below) which seems to be rather for software development and debugging and not at all for what i am trying. And somepoint there seem to be 3rd party solutions but they are no longer continued in favor of this Testing framework.

function solutions = sampleTest
    solutions = functiontests({@paramtertest});
end

function paramtertest(vargin)
    test(1,'a');
    test(2,'b');
end

function [number2x,next_letter] = test(number, letter) 

    number2x = number * 2;
    next_letter = letter + 1;
    disp(['next letter is ' next_letter])
    disp(['number times 2 is ' num2str(number2x)])

    xlswrite('testfile.xlsx',[num2str(number), letter,num2str(number2x),next_letter],'append');
end

Alternatively I could create my test as a class, create an interface similar to the Simulationmanager, create numerous functions for managing inputs and outputs and visualize the progress and then spawn multiple instances of if i want to set up a new set of parameters while already running a simulation. Possible, yet a lot of work that does not involve the simulation directly.

In total following questions arise:

  • Is there a build in Matlab function for managing simulations that i totally missed so far?
  • Can the the Testing framework be used for this purpose?
  • Is there already some Framework (not from Mathworks) that can handle this?
  • If i create my own class, could multiple instances run individually and keep track of their own progress? And would those be handled simultaniously or would matlab end up running the in the order they started?

I know this question is somewhat in the off-topic: recommend or find a tool, library or favorite off-site resource area. If you feel it is too much so, please focus on the last question.

Thank you!

Finn
  • 2,333
  • 1
  • 10
  • 21

1 Answers1

0

I've done similar tests using GUI elements. Basic part of simulation was inside while loop, for example in your case:

iter = 0;
iter_max = 5; %the number of your times, you will call script
accu_step = 2; %the accuracy of stored data
Alphabet = 'abcdefghijklmnopqrstuvwxyz'
while iter < iter_max
    iter = iter+1;
    [x1,y1] = test(i,Alphabet(i));
end

Now you should create a handle to progress bar inside your computation script. It will show you both on which step you are, and the progress of current step.

global h;
global iter_opt;
if isempty(h)
  h=waitbar(0,'Solving...');
else
  waitbar(t/t_end,h,sprintf('Solving... current step is:%d',iter));
end

You didn't specified which function you use, if it is for example time-depended the above t/t_end example is an estimation of current progress.

The solving of result also require to be changed on every execution of loop, for example:

global iter;
i_line = (t_end/accu_step+2)*(iter-1);
xlswrite('results.xlsx',{'ITERATION ', iter},sheet,strcat('A',num2str(i_line+5)))
xlswrite('results.xlsx',results_matrix(1:6),sheet,strcat('D',num2str(i_line+5)))

The above example were also with assumption that your results are time-related, so you store data every 2 units of time (day, hours, min, what you need), from t_0 to t_end, with additional 2 rows of separation, between steps. The number of columns is just exemplary, you can adjust it to your needs.

After the calculation is done, you can close waitbar with:

global h
close(h)
Karls
  • 731
  • 7
  • 17