Share variables between two MATLAB m-files executing separately - matlab

I'd like to know if there is something like pointers in MATLAB.
I have two matlab routines which execute simulataneously (that is they are lunched together and run on the same machine, which is, therefore, synchronized in terms of time-stamps).
The first routine, A, has a parfor loop where a data vector is continuously updated. The second routine, B, needs to get access to a specific (but variable in time) row of the vector (of routine A) and do further calculation.
My first guess is to print the updating rows of routine A in a txt file and then get access to it in B when necessary. However, this will result in a large amount of waste time.
I know that this could be related to parallel jobs and scheduler but I dont know how to implement it.
Thank you for any help, guesses or solutions.

Probably this code will help you, if i got correctly your problem. I am waiting for further comments.
Best wishes!

Related

Doubts with parfor in Matlab?

Could you explain me why
the following code with parfor in Matlab does not work and how to fix it?
R=10;
Power=zeros(2,R);
parfor s=1:R
Power(1,s)=1
Power(2,s)=2;
end
It doesn't work because you have 1 variable that is sent to different workers (power) and you want to write on it using different cores.
How can you write in the same variable with different workers? who stores the memory? How do workers communicate where did they write and where not? The structure of the code is very important when doing parallel computing, as you need to be aware of what memory you send to what worker. Just choosing a wrong approach in passing variables can make your code slower than the non-parallel one.
The code you show can be changed to:
R=10;
Power1=zeros(1,R);
Power2=zeros(1,R);
parfor s=1:R
Power1(1,s)=1
Power2(1,s)=2;
end
Power=[Power1;Power2]
I suggest you go to http://uk.mathworks.com/help/distcomp/parallel-for-loops-parfor.html
and read the "concepts" section, especially the variable types in parfors, that the MATLAB error directs you to.

How to vectorize signal and parameter?

I created a subsystem in Simulink with mask underneath. There are all sorts of control and calculation inside this subsystem. Now I have to duplicate this subsystem for one hundred thousand times because I need to connect one hundred thousands of this block in series.
What I have tried, I used the commands “add_block” and “add_line” where I can just type it in the Matlab command and the blocks and lines are added automatically.
What I wish to do now is,
I want to have 100 signals in a single subsystem, so instead of using one hundred thousand subsystem, I will only need one thousand of this subsystem, I understand that this can be done by vectorization.
I have a very limited knowledge on using vectorization feature in Matlab/Simulink. I appreciate if anyone of you could provide me a great reference on how to do this?
What I found here is something like this which I could not link it to my issue above: http://www.mathworks.co.uk/help/matlab/matlab_prog/vectorization.html
The other thing I found is by "using vectorization for most components. Most components are vectorized if they have a vectorized input signal or if one of their parameter is specified as a vector."
However, I could not find any further information/details, appreciate if anyone of you could give opinion on this? Thanks!

How to get the iteration number of lsqnonlin

I am doing parameter estimation in matlab using lsqnonlin function.
In my work, I need to plot a graph to show the error in terms of lsqnonlin iteration. So, I need to know which iteration is running at each point of time in lsqnonlin. Could anybody help me how I can extract the iteration number while lsqnonlin is running?
Thanks,
You want to pass it an options parameter setting 'display' to either 'iter' or 'iter-detailed'
http://www.mathworks.com/help/optim/ug/lsqnonlin.html#f265106
Never used it myself, but looking at the help of lsqnonlin, it seems that there is an option to set a custom output function, which gets called during every iteration of the solver. Looking at the specification, it seems that the values optimValues.iteration and optimValues.fval get passed into the function, which is probably the things you are interested in.
You should thus define your own function with the right signature, and depending on your wishes, this function prints it on the command line, makes a plot, saves the intermediate results in a vector, etc. Finally, you need to pass this function as a function handle to the solver: lsqnonlin(..., 'OutputFcn', #your_outputfun).
The simple way to do this would be:
Start with a low number of (maximum) iterations
Get the result
Increase the number of iterations
Get the result
If the maximum iterations is used Go to step 3
This is what I would recommend in most cases when performance is not a big issue.
However, if you cannot afford to do it like this, try edit lsqnonlin and go digging untill you find the point where the number of iterations is found. Then change the function to make sure you store the results you need at that point. (don't forget to change it back afterwards).
The good news is that all relevant files seem to be editable, the bad news is that it is not so clear where you can find the current number of iterations. A quick search led me to fminbnd, but I did not manage to confirm that this is actually used by lsqnonlin.

Implementation of Iterative Learning Control in Matlab/Simulink

I am working on the development of an Iterative Learning Controller for a simple transfer function.
The iterations are controlled by the external matlab loop.
But the error e(k) (k is trial number) is not updating ... as the trials increases.
Please detect the error I've commited.
Thanks and Regards.
You might have solved the problem. But as the question is still open, I would like to add something here.
First of all, you might want to check the usage of "memory" block. "The Memory block holds and delays its input by one major integration time step." The reason why the error wasn't updating is that the output of your plant produced was the same in each iteration(you defined external loop). The memory block only delayed one step of your U(K), not the whole iteration.
You might want to store the error of each iteration to workspace, and use it for the next iteration.
The memory should be a vector with a lenght of the single iteration. Not just single value. Delay block can store multiple past samples.
This guy did probably what you were looking for: https://github.com/arthurrichards77/iterative-learning-control

Need a method to store a lot of data in Matlab

I've asked this before, but I feel I wasn't clear enough so I'll try again.
I am running a network simulation, and I have several hundreds output files. Each file holds the simulation's test result for different parameters.
There are 5 different parameters and 16 different tests for each simulation. I need a method to store all this information (and again, there's a lot of it) in Matlab with the purpose of plotting graphs using a script. suppose the script input is parameter_1 and test_2, so I get a graph where parameter_1 is the X axis and test_2 is the Y axis.
My problem is that I'm not quite familier to Matlab, and I need to be directed so it doesn't take me forever (I'm short on time).
How do I store this information in Matlab? I was thinking of two options:
Each output file is imported separately to a different variable (matrix)
All output files are merged to one output file and imprted together. In the resulted matrix each line is a different output file, and each column is a different test. Problem is, I don't know how to store the simulation parameters
Edit: maybe I can use a dataset?
So, I would appreciate any suggestion of how to store the information, and what functions might help me fetch the only the data I need.
If you're still looking to give matlab a try with this problem, you can iterate through all the files and import them one by one. You can create a list of the contents of a folder with the function
ls(name)
and you can import data like this:
A = importdata(filename)
if your data is in txt files, you should consider this Prev Q
A good strategy to avoid cluttering your workspace is to import them all into a single matrix. SO if you have a matrix called VAR, then VAR{1,1}.{1,1} could be where you put your test results and VAR{1,1}.{2,1} could be where you put your simulation parameters of the first file. I think that is simpler than making a data structure. Just make sure you uniformly place the information in the same indexes of the arrays. You could also organize your VAR row v col by parameter vs test.
This is more along the lines of your first suggestion
Each output file is imported separately to a different variable
(matrix)
Your second suggestion seems unnecessary since you can just iterate through your files.
You can use the command save to store your data.
It is very convenient, and can store as much data as your hard disk can bear.
The documentation is there:
http://www.mathworks.fr/help/techdoc/ref/save.html
Describe the format of text files. Because if it has a systematic format then you can use dlmread or similar commands in matlab and read the text file in a matrix. From there, you can plot easily. If you try to do it in excel, it will be much slower than reading from a text file. If speed is an issue for you, I suggest that you don't go for Excel.