How to copy a variable from Matlab -workspace to Simulink? - matlab

I want to get the value of C in Matlab to Simulink, I could do it by using C but I want to get it as a code so not dependent on environment values. So how to get the matrix X there from Matlab to Simulink?

If I understand correctly your needs, the best is to define your constants in a file (.m or .mat) and call this file automatically using the InitFcn callback (see doc).

Well if you can input A and B, then it should not be hard to input C.
Try inputting the values with these rules:
[ ] Brackets to open and close
, Comma to seperate elements on one row
; Semicolon to separate two rows
Example
C = [ -1/3 0 0 -1/90 0.0037; 6 7 8 9 10; 11 12 13 14 15]

Related

Double for loop in MATLAB, storing the information

I have two for loops in MATLAB.
One of the for loops leads to different variables being inserted into the model, which are 43 and then I have 5 horizons.
So I estimate the model 215 times.
My problem is I want to store this in 215x5 matrix, the reason I have x5 is that I am estimating 5 variables, 4 are fixed and the other one comes from the for loop.
I have tried to do this in two ways,
Firstly, I create a variable called out,
out=zeros(215,5);
The first for loop is,
for i=[1,2,3,4,5];
The second for loop is,
for ii=18:60;
The 18:60 is how I define my variables using XLS read, e.g. they are inserted into the model as (data:,ii).
I have tried to store the data in two ways, I want to store OLS which contains the five estimates
First,
out(i,:)=OLS;
This method creates a 5 x 5 matrix, with the estimates for one of the (18:60), at each of the horizons.
Second,
out(ii,:)=OLS;
This stores the variables for each of the variables (18:60), at just one horizon.
I want to have a matrix which stores all of the estimates OLS, at each of the horizons, for each of my (18:60).
Minimal example
clear;
for i=[1,2,3,4,5];
K=i;
for ii=18:60
x=[1,2,3,i,ii];
out(i,:)=x;
end
end
So the variable out will store 1 2 3 5 60
I want the variable out to store all of the combinations
i.e.
1 2 3 1 1
1 2 3 1 2
...
1 2 3 5 60
Thanks
The simplest solution is to use a 3D matrix:
for jj=[1,2,3,4,5];
K=jj;
for ii=18:60
x=[1,2,3,jj,ii];
out(ii-17,jj,:)=x;
end
end
If you now reshape the out matrix you get the same result as the first block in etmuse's answer:
out = reshape(out,[],size(out,3));
(Note I replaced i by jj. i and ii are too similar to use both, it leads to confusion. It is better to use different letters for loop indices. Also, i is OK to use, but it also is the built-in imaginary number sqrt(-1). So I prefer to use ii over i.)
As you've discovered, using just one of your loop variables to index the output results in most of the results being overwritten, leaving only the results from the final iteration of the relevant loop.
There are 2 ways to create an indexing variable.
1- You can use an independent variable, initialised before the loops and incremented at the end of the internal loop.
kk=1;
for i=1:5
for ii=18:60
%calculate OLC
out(kk,:)=OLC;
kk = kk+1;
end
end
2- Use a calculation of i and ii
kk = i + 5*(ii-18)
(Use in loop as before, without increment)

Function which no longer exists

I am interested in using the function here:
http://uk.mathworks.com/help/nnet/ref/removerows.html
However, when I try to use it in Matlab it says: "Undefined function or variable 'removerows'"
I typed: exist removerows and returned a value of 0, suggesting that it's been removed. Has this function just been renamed? or is it part of a toolbox I may not have, the information does not detail this.
Much appreciated
According to the link that you posted, this function is part of the Neural Network Toolbox. So my guess is that you don't have this toolbox installed.
You can remove rows in a matrix by assigning an empty array to them.
This way you don't have to use functions belonging to toolboxes that require extra licences.
Example
A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
A(2,:) = [] %remove row 2
A =
1 2
5 6
Similarly you can provide an index array with the rows to be deleted in case you want to remove several ones.

how to separate the figures of specific number using matlab?

How do I separate the figures of specific number then make a new vecto?. This vector will include the figures that have been separated separately.
For example : if i have a number as 123456789 , what is the function or command that will separate these figures of the number so that they look like this form [1,2,3,4,5,6,7,8,9] Meaning that will turn into a vector.
Use dec2base to obtain the figures as a char vector (i.e. string), and then convert those chars into numbers with the usual trick of subtracting '0':
>> number = 123456789;
>> figures = dec2base(number,10)-'0'
figures =
1 2 3 4 5 6 7 8 9

Creating a MIMO (multiple input multiple output) transfer function system without hardcoding the number of inputs and outputs

Introduction
As part of a larger system I'm trying to create a multiple input multiple output transfer function that only links inputs to outputs on the lead diagonal*. I.e. it has non zero transfer functions between input 1 and output 1, input 2 and output 2 etc etc.
*whether you really count that as a MIMO system is a fair comment, I want it in this format because it links to a larger system that really is MIMO.
Hard Coding
I can achieve this by concatenating transfer functions as so
tf1=tf([1 -1],[1 1]);
tf2=tf([1 2],[1 4 5]);
tf3=tf([1 2],[5 4 1]);
G=[tf1 0 0; 0 tf2 0; 0 0 tf3];
Which works fine, but (a) hard codes the number of inputs/outputs and (b) becomes increasingly horrible the more inputs and outputs you have.
Diag function
This problem seemed perfect for the diag function however diag does not seem to be defined for type 'tf'
G=diag([tf1, tf2, tf3])
??? Undefined function or method 'diag' for input arguments of type 'tf'.
Manual Matrix manipulation
I also tried manually manipulating a matrix (not that I was really expecting it to work)
G=zeros(3);
G(1,1)=tf1;
G(2,2)=tf2;
G(3,3)=tf3;
??? The following error occurred converting from tf to double:
Error using ==> double
Conversion to double from tf is not possible.
tf's direct to MIMO format
tf also has a format in which all the numerators and denominators are represented seperately and a MIMO system is directly created. I attempted to use this in a non hard coded format
numerators=diag({[1 -1], [1 2],[1 2]})
denominators=diag({[1 1], [1 4 5],[5 4 1]})
G=tf( numerators , denominators )
??? Error using ==> checkNumDenData at 19
Numerators and denominators must be specified as non empty row vectors.
This one almost worked, unfortunately numerators and denominators are empty on the off diagonal rather than being 0; leading to the error
Question
Is it possible to create a MIMO system from transfer functions without "hard coding" the number of inputs and outputs
I suggest you try realizing each SISO as a state space system, say (Ak, Bk, Ck, Dk), assembling a large diagonal system like
A = blkdiag(A1,....)
B = blkdiag(B1,...)
C = blkdiag(C1,...)
D = diag([D1, ....])
and then use ss2tf to compute the transfer function of the augmented system.
diag in matlab is not the same as blkdiag. The overloaded LTI operator is the blkdiag to put things on a diagonal of a matrix structure.
In your case, it is done simply by
tf1=tf([1 -1],[1 1]);
tf2=tf([1 2],[1 4 5]);
tf3=tf([1 2],[5 4 1]);
G = blkdiag(tf1,tf2,tf3)
The MIMO syntax requires cells to distinguish the polynomial entries from the MIMO structure. Moreover, it does not like identically zero denominator entries (which is understandable) hence if you wish to enter in the mimo context you need to use
G = tf({[1 -1],0,0;0,[1 2],0;0,0,[1 2]},{[1 1],1,1;1,[1 4 5],1;1,1,[5 4 1]})
or in your syntax
Num = {[1 -1],0,0;0,[1 2],0;0,0,[1 2]};
Den = {[1 1],1,1;1,[1 4 5],1;1,1,[5 4 1]};
tf(Num,Den)
Instead of ones you can basically use anything valid nonzero entries.

How to set custom seed for pseudo-random number generator

I need to perform few tests where I use randn pseudo random number generator. How can I set the seed on my own, so every time I run this test I will get the same results? (yeah, I know it's a little bit weird, but that's the problem).
I've found the RANDSTREAM object that has the seed property, but it's read only. Is there any way to use it for seeding the generator?
The old way of doing it:
randn('seed',0)
The new way:
s = RandStream('mcg16807','Seed',0)
RandStream.setDefaultStream(s)
Note that if you use the new way, rand and randn share the same stream so if you are calling both, you may find different numbers being generated compared to the old method (which has separate generators). The old method is still supported for this reason (and legacy code).
See http://www.mathworks.com/help/techdoc/math/bsn94u0-1.html for more info.
You can just call rng(mySeed) to set the seed for the global stream (tested in Matlab R2011b). This affects the rand, randn, and randi functions.
The same page that James linked to lists this as the recommended alternative to various old methods (see the middle cell of the right column of the table).
Here's some example code:
format long; % Display numbers with full precision
format compact; % Get rid of blank lines between output
mySeed = 10;
rng(mySeed); % Set the seed
disp(rand([1,3]));
disp(randi(10,[1,10]));
disp(randn([1,3]));
disp(' ');
rng(mySeed); % Set the seed again to duplicate the results
disp(rand([1,3]));
disp(randi(10,[1,10]));
disp(randn([1,3]));
Its output is:
0.771320643266746 0.020751949359402 0.633648234926275
8 5 3 2 8 2 1 7 10 1
0.060379730526407 0.622213879877005 0.109700311365407
0.771320643266746 0.020751949359402 0.633648234926275
8 5 3 2 8 2 1 7 10 1
0.060379730526407 0.622213879877005 0.109700311365407
mySeed=57; % an integer number
rng(mySeed,'twister') %You can replace 'twister' with other generators
When you just want to reset the RNG to some known state, just use:
seed = 0;
randn('state', seed);
rand ('state', seed);
A = round(10*(rand(1,5))); // always will be [10 2 6 5 9]