Can't output multidimensional matrix in Matlab function in Simulink - matlab

For example I have function that creates matrix 2x2 : [1 2; 3 4]
I have such simple function:
function[result] = Rho(x)
// I've tried and so:
result = [1 2; 3 4];
// And so:
result(1,1) = 1;
result(1,2) = 2;
result(2,1) = 3;
result(2,2) = 4;
In Matlab window i see right result:
>> Rho(1)
ans =
1 2
3 4
But in Simulink I always get [1;2;3;4]. Where is my mistake?
P.S. I forgot to remove argument x of function, because in real function I make matrix depending on argument x. But it doesn't play role in our example

The problem you are having is likely due to the parameter settings for your MATLAB Function block (now called an Interpreted MATLAB Function block in newer versions). Take a look at the Parameters Dialog Box for that block:
Note that you will want to set the Output dimensions to 2 and uncheck the Collapse 2-D results to 1-D check box. If this is left checked, then your 2-by-2 matrix will be turned into a 1-D array by extracting values along each column from left to right, which ends up being [1 3 2 4] in your example.
Once you apply the above changes, then all you should have to do is resize your Display block so that it shows your 2 rows and 2 columns.

Related

Multiple discrete values in MATLAB graph

I have some data that I'd like to plot on a graph in MATLAB.
the data is discrete - specifically these are 2 series of data against a single vector.
I could easily do it in excel like this:
but I want to do it in matlab. I tried to use the stem function, but the 2 series values are shown on the same bar (and I want them side by side, like the excel does):
In addition, I would like to show on the x bar only the values I'm interested in (in my case: 2,4,8,16,32). How do I do that?
Since you want to draw a bar graph, there is a dedicated built-in function, named bar(), for that purpose.
You can do it using:
N = [2 4 8 16 32];
val1 = [1; 2; 3; 4; 5];
val2 = [3; 5; 6; 12; 17];
bar([N],[val2,val1]); % If you want val1 to appear first then use bar([N],[val1,val2]);
which gives the following desired result:

Using matlab function in simulink error

I'm new to Simulink but I need to use a matlab function.
I created a "MATLAB Function1" block with one input (a time signal that comes from another block) and one output (three signals wrapped in a matrix shown in a Scope block).
Here the code inside the matlab function block:
function outputSignal = myFunction(input_signal)
coder.extrinsic('lsim');
time = [1:1:length(input_signal)];
k_dia = [19.5 13 9.9];
k_dia = k_dia*10^-3;
outputSignal = zeros(length(time), length(k_dia));
for j = 1:length(k_dia)
A = [-k_dia(j) 0; k_dia(j) -k_dia(j)];
B = [1 0]';
C = [1 1];
D = 0;
sistem = feval('ss', A, B, C, D);
outputSignal(:,j) = lsim(sistem, input_signal, time);
end
end
Previously I had problems using the functions "ss" and "lsim" because of code generation problems, but I should have solved them using feval and coder.extrinsic. Now I have the following error:
When simulating the response to a specific input signal, the input data U must be a matrix
of numeric values with at least two rows (samples) and without any NaN or Inf.
and I can't understand if the problem is still with these functions or if I made a mistake in how to use matlab functions in simulink.
EDIT: I understood that the problem was because lsim needs an input_signal of at least length 2, while my system is giving an input to the function of one single value at every time step. So if the time is of 10 steps and the serie of values generated from the previous block is [1 2 3 4 5 6 7 8 9 10], I would like to have as input to my function:
[1] at step 1,
[1 2] at step 2,
[1 2 3] at step 3,
....
[1 2 3 4 5 6 7 8 9 10] at step 10.
Of course since lsim doesn't work with one value, at step 1 I could use a default output value.
I think I need a block that memorizes what happens in the time steps
before, something like the Scope block but with an output.
Since your systems are time invariant you can create three separate State space systems each with different parameter valued matrices and supply a vector of three components as input signals which I have left empty because I don't know where you would like to send from.

frequency of each vector value in another vector matlab

I need to calculate the frequency of each value in another vector in MATLAB.
I can use something like
for i=1:length(pdata)
gt(i)=length(find(pf_test(:,1)==pdata(i,1)));
end
But I prefer not to use loop because my dataset is quite large. Is there anything like histc (which is used to find the frequency of values in one vector) to find the frequency of one vector value in another vector?
If your values are only integers, you could do the following:
range = min(pf_test):max(pf_test);
count = histc(pf_test,range);
gt = count(ismember(range,a));
gt(~ismember(unique(a),b)) = 0;
If you can't guarantee that the values are integers, it's a bit more complicated. One possible method of it would be the following:
%restrict yourself to values that appear in the second vector
filter = ismember(pf_test,pdata);
% sort your first vector (ignore this if it is already sorted)
spf_test = sort(pf_test);
% Find the first and last occurrence of each element
[~,last] = unique(spf_test(filter));
[~,first] = unique(spf_test(filter),'first');
% Initialise gt
gt = zeros(length(pf_test));
% Fill gt
gt(filter) = (last-first)+1;
EDIT: Note that I may have got the vectors the wrong way around - if this doesn't work as expected, switch pf_test and pdata. It wasn't immediately clear to me which was which.
You mention histc. Why are you not using it (in its version with two input parameters)?
>> pdata = [1 1 3 2 3 1 4 4 5];
>> pf_test = 1:6;
>> histc(pdata,pf_test)
ans =
3 1 2 2 1 0

Combination of colon-operations in MATLAB

I have a question concerning the colon operator and expansion of vectors in MATLAB. My problem is to understand how the following line of code expands, to be able to use it for other sequences. The line of MATLAB code is:
a(1:2:5) = 1:-4:-7
Note that a is not defined before the expansion. This returns the vector
a = 1 0 3 0 -7
I know how the colon operator works with {start}:{step}:{stop}, my problem is to understand how and why the combination of a(1:2:5)and 1:-4:-7 returns a vector of five elements with zeros in position 2 and 5?
Whenever Matlab detects you're indecing to an element outside the current bounds of the matrix/array, it will automatically pad the missing elements with zeros:
>> clear b; b(10) = 5
b =
0 0 0 0 0 0 0 0 0 5
This feature is both very useful, and very dangerous. It is useful for the fact declarations can be made very easy, such as your own case. You can create a whole array of custom-made classes by issuing something like
myClassArray(500) = myClass(1, 2);
which is infinitely better than something like
% cannot pre-allocate (zeros() or ones() give double/uint8/..., not myClass)
for ii = 1:499
myClassArray(ii) = myClass; % so, growing array
end
myClassArray(500) = myClass(1,2);
But, growing arrays can be hard to spot:
a = zeros(10,1);
for ii = 1:10
a(ii+1) = rand;
end
which can make performance drop tremendously. Also, when you translate code prototyped in Matlab to a statically-typed language like C++, copying this code will result in buffer overflows and thus segfaults.
Now, going back to your case:
clear a; a(1:2:5) = 1:-4:-7
The 1:2:5 will expand to the array [1 3 5], and the 1:-4:-7 will give the values [1 -3 -7]. Since the variable a does not exist yet, Matlab will create a new one and fill the elements [1 3 5] with the values [1 -3 -7]. The indices that have been skipped in order to initialize variable a (namely, [2 4]) will then have been initialized automatically to zero.
If you're familiar with Python, it's a bit like the syntax to assign multiple values to multiple variables
x,y = 1,2
But in your Matlab case, these different variables are indices to a non-existent array, which requires "filling the holes with something" to make it a valid, consistent array.
Does this make things clear?
when you define a(1:2:5), it creates a size 5 vector (zero-valued), and selecting odd indexed(3 of them exists) cells. 1:-4:-7 creates three values (not five). Finally your selected three cells are filled with data of 3 values coming from 1:-4:-7

Load values from a text file in MATLAB

I would like to load variables from a text file.
For example, my text file, varA, varB, and varC.
In MATLAB, I would like to give these variables values so that every variable is a 2x2 matrix.
So from the text file containing the above information I would get a matrix that looks like this:
[ 1 2 3 4 5 6;
1 2 3 4 5 6]
Is this possible?
I added a second example to try to make things a little clearer.
My text file, text.txt, looks like this
x1 x2 x3
In MATLAB my .m file gives the values to these variables like
x1 = [1 1; 1 1]
x2 = [2 2; 2 2]
x3 = [3 3; 3 3]
So, when I import my textfile I would get
a = (textfile)
a = [1 1 2 2 3 3 ; 1 1 2 2 3 3]
I basically try to adapt a genetic algorithm (GA) on a very huge problem (of travelling salesman problem (TSP) type). The problem is that every variable I have is a matrix and the crossover, fitness and mutation codes get pretty complicated. And I am having problems of making a random start population as well.
I would like to randomly select, let's say 30 variables, from a list with 256 so that the variable can only be picked once. Each variable however have their own specific values in a 2*2 matrix that cannot be changed.
I would like to use randperm and then put an x before every value making them variables instead of values...
If the data in the text file looks like this (strings separated by spaces):
x1 x2 x3 ...
You can read the strings into a cell array using TEXTSCAN like so:
fid = fopen('file.txt','r');
A = textscan(fid,'%s');
fclose(fid);
A = A{:};
A now stores the strings in a cell array: {'x1'; 'x2'; 'x3'; ...}. Now, to make a variable out of one of these strings and assign it a value, I would use ASSIGNIN:
assignin('base',A{1},[1 2; 1 2]);
This will create a variable x1 in the base workspace and assign it the value [1 2; 1 2]. The first argument can be either 'base' or 'caller' to create a variable in either the MATLAB base workspace or the workspace of the caller function. You would repeat this for each string name in A, giving it whatever value you want.
ALTERNATE OPTION:
This is an alternate answer to the one I gave you above. The above answer addresses the specific problem you raised in your question. This answer gives you a whole other option to potentially avoid doing things the way you were describing them in your question, and it will hopefully make things easier for you...
If I understand your problem, you basically have 256 2-by-2 matrices, and you want to randomly pick 30 of them. Each of these 2-by-2 matrices sounds like it is stored in its own variable (x1 to x256). Instead, I would suggest storing all 256 matrices in just one variable as either a 3-D array:
xArray = zeros(2,2,256); % Initialize all matrices as [0 0; 0 0]
xArray(:,:,1) = [1 1; 2 2]; % This enters a value for the first matrix
or a cell array:
xArray = cell(1,256); % Initializes an empty array of cells
xArray{1} = [1 1; 2 2]; % Enters a value for the first matrix
You would have to initialize all the values first. Then if you want to randomly pick 30 values, you can next randomize the order of either the third dimension of the 3-D array or the order of the cell array by using RANDPERM:
startOrder = 1:256; % The default order of the matrices
index = randperm(256); % Randomly order the numbers 1 to 256
xArray = xArray(:,:,index); % For a 3-d array
xArray = xArray(index); % For a cell array
Then just use the first 30 entries in xArray for your calculations (instead of the individual variables like you were before):
x = xArray(:,:,1); % Puts the first matrix from the 3-D array in x
x = xArray{1}; % Puts the first matrix from the cell array in x
You can keep repeating the use of RANDPERM to keep generating new randomized arrays of matrices. If you have to keep track of which original matrices you are using, you have to add this line after you randomize xArray:
startOrder = startOrder(index);
Now the entries of startOrder will tell you the original position a matrix was in. For example, if the first array entry in startOrder is 40, then the matrix in the first position of xArray was originally the 40th matrix you entered when you initialized xArray.
Hope this helps!