I'm porting matlab code to python and come across the code below. Looks like it creates a matrix but I'm not sure what the shape of the matrix would be. Can anybody help me understand what this code mean especially '...' and '].^2'?
somevarialbe = [var1...
var2...
var3].^2;
It is the item-wise operator and power each item to 2. In the other words, it is equivalent to the following code:
somevarialbe = [var1^2...
var2^2...
var3^2];
And ... means next line in the code. Hence, it is equivalent to the following code:
somevarialbe = [var1^2 var2^2 var3^2];
Related
I'd like to create a simple Simulink model containing a "MATLAB Function" block programmatically -- i.e. using Matlab code.
Thanks to this guide, I've managed to create a new model containing the block:
open_system(new_system('my_system'))
add_block('simulink/User-Defined Functions/MATLAB Function', 'my_system/my_func')
Usually, in order to edit the "MATLAB Function" block's code, one has to "open" the block by double-clicking on it then entering the new code.
However, I would like to set that code programmatically using e.g. set_param() or any relevant function.
For instance, to set the following as the block's code:
function y = fcn(v)
%#codegen
y = 2 * u;
I would like to use something like:
set_param('my_system/my_func', 'Script',...
'function y = fcn(u)\n%#codegen\n\ny = 2 * u;'...
);
I've looked at the output of get_param('my_system/my_func', 'ObjectParameters') and tried to guess which parameter might be used to set the block's function code: So far, I couldn't find any. Therefore, my question is:
Q: Is it possible, using only Matlab commands, to set the code of a "MATLAB Function" block in Simulink?
(As requested by #Ander Biguri, I've moved a solution that worked for me to a separete answer post. If anyone has an alternative/better approach, please feel free to post it as well)
Well, it seems that this question was asked here before. (perhaps formulated differently though?) I've managed to solve my issue using the following code:
sf = sfroot()
block = sf.find('Path','my_system/my_func','-isa','Stateflow.EMChart');
block.Script = sprintf('function y = fcn(u)\n%%#codegen\n\ny = 2 * u;')
My question is a little bit stupid, but still, I would like someone who can help me if he/she face the same problem as me.
I copy the codes directly from MATLAB mathwork:
anonrosen = #(x)(100*(x(2) - x(1)^2)^2 + (1-x(1))^2);
opts = optimoptions(#fmincon,'Algorithm','interior-point');
problem = createOptimProblem('fmincon','x0',randn(2,1),...
'objective',anonrosen,'lb',[-2;-2],'ub',[2;2],'options',opts);
However, it gives the following error message:
Undefined function 'createOptimProblem' for input arguments of type 'optim.options.Fmincon'.
createOptimProblem should be a built-in function, but with the presence of the error message, I wonder if I need to declare sth before using createOptimProblem, what should I do?
I am using R2013a version
I've got the error of "Error using histc. First Input must be a real non-sparse numeric array" from the following codes.
N=10^4;
d=rand(1,N)>0.5;
symbols=unique(d);
probs = histc(d,symbols)./numel(d);
P/s: I try to generate using randsrc before. It did worked.But, I'm hoping not to use randsrc because it will affect my code later on. Any ideas on this would be appreciated.
Thanks.
Here is the working code
N=10^4;
d=double(rand(1,N)>0.5);
symbols=unique(d);
probs = histc(d,symbols)./numel(d);
I wrote code similar to that posted by FeliceM, but when I tried to include the recommended changes/additions I got the following error:
Warning: File: odd_index.m Line: 5 Column: 18
Function with duplicate name "odd_index" cannot be called.
Here's my code:
function odd_index
M=[1:5; 6:10; 11:15; 16:20; 21:25];
M=M(1:2:end, 1:2:end);
end
function M_out = odd_index(M)
M_out = M(1:2:end, 1:2:end);
end
Not quite sure what I'm doing wrong.
It appears to be simply a problem of naming your two functions the same name. Rename the second function (and make sure it's in a separate file with the same name as itself). Really, the first one isn't a function at all, but appears to be a script.
You may want to refer to the MATLAB Getting Started help documentation for more information on functions and scripts. http://www.mathworks.com/help/matlab/getting-started-with-matlab.html
I've been looking throught the documentation, but can't seem to find the bit I want.
I have a for loop and I would like to be able to view every value in the for loop.
for example here is a part of my code:
for d = 1 : nb
%for loop performs blade by blade averaging and produces a column vector
for cc = navg : length(atbmat);
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg;
atbvec2(:,cc) = atb2;
end
%assigns column vector 'atbvec2' to the correct column of the matrix 'atbmat2'
atbmat2(d,1:length(atbvec2)) = atbvec2;
end
I would like to view every value of atb2. I'm a python user(new to MATLAB) and would normally use a simple print statement to find this.
I'm sure there is a way to do it, but I can't quite find how.
Thankyou in advance.
you can use disp in Matlab to print to the screen but you might want to use sprintf first to format it nicely. However for debugging you're better off using a break point and then inspect the variable in the workspace browser graphically. To me, this is one of Matlab's best features.
Have a look at the "Examine Values" section of this article
The simplest way to view it everywhere is to change this line:
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg;
Into this, without semicolon:
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg
That being said, given the nature of your calculation, you could get the information you need as well by simply storing every value of abt2 and observing them afterwards. This may be done in atbmat2 already?
If you want to look at each value at the time it happens, consider setting a breakpoint or conditional breakpoint after the line where abt2 is assigned.