MATLAB: how to reuse plot options? [duplicate] - matlab

This question already has answers here:
Is that possible to assemble several options and pass to the plot function in matlab
(4 answers)
Closed 7 years ago.
I have the following plot:
patch('Vertices',rocket_point_cloud,'Faces',rocket_faces,...
'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1)
I would like to reuse the plot options, i.e. reuse:
'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1
Is it somehow possible to store the above in a variable, e.g. my_options and later on do:
patch('Vertices',other_cloud,'Faces',other_faces,my_options)
Thanks for your help!

Sure. Just define your options in a cell array,
my_options = {'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1};
and then expand that cell array into a comma-separated list via curly-brace indexing:
patch('Vertices', rocket_point_cloud, 'Faces', rocket_faces, my_options{:})

Related

In Matlab, how to quickly select single element of matrix produced by a function? [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 8 years ago.
E.g. I have the output of cov(A,B), which is a 2×2 matrix.
I want to select the element in position 2,1 of the matrix.
I can do this by blah = cov(A,B) and then select blah(1,2).
This isn't the most efficient way to do it though, and I'd prefer to do it in one line. Is there a way to do that?
You can try using getfield():
getfield(cov(A,B), {1,2})
The performance difference between this and what you have currently will likely be negligible, however. I personally would prefer just using that temporary variable.
<stealing brilliance from Amro>
You can also do this:
C = builtin('_paren', cov(A,B), 2, 1);
</stealing brilliance from Amro>

Access second output of Matlab procedure directly [duplicate]

This question already has answers here:
How to elegantly ignore some return values of a MATLAB function
(8 answers)
How do I get the second return value from a function without using temporary variables?
(2 answers)
Closed 9 years ago.
I'm not very good at programming and new to matlab, so sorry if I'm not using the right terminology.
If I use the e.g. fminbnd procedure in Matlab first i get the x-value which minimises and then i get the function value. Is there a neat way for me to get just the minimum function value.
To make it clear, for me it seems I have to do:
[x,y] = fminbnd(h,-10,10)
when I only need y. Is there any way for me to not get x?
Use ~ to suppress x output. Only available in later versions of matlab (=> r2009b).
[~, y] = fminbnd(h, -10, 10);

Quicker way to access a particular column of a function result in matlab [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 9 years ago.
I was asking myself if there was a quicker way to do this in matlab :
Imagine we have a 10x2 vector V and we want to use the x dimension (number of lines, here 10) in a function or do whatever we want with it. The way I usually do it is this :
[x y]=size(V);
function(x)
But would it be possible to make it differently? Soemething like
function(size(V)(1))
Thanks for your help !
MATLAB's size can take a second input argument, indicating the dimension of which you would like to know the size. The output is scalar in that case:
x = size(V,1);
y = size(V,2);
See help size for more details.

Creating a list of matrices [duplicate]

This question already has answers here:
Array of Matrices in MATLAB
(6 answers)
Closed 9 years ago.
I am new to programming and I was wondering if my question has a simple implementation. I have a bunch of matrices and I want a way to be able to store them, or be able to easily call them and do operations on them. For example, if I have 100 matrices, called, M1,M2,...M100; is there a way I can rename them so that if I want to call the nth matrix, I can just write M(nth)?
EDIT:
For example, if I want to add M1+M1, M1+M2, ...,M1+M100; I want to be able to write a loop something kind of like,
for i=1:100
AM(i)=M(1)+M(i)
end
Is this possible?
Use cell array
AM = cell(1,100);
and set it as
AM{i} = Mi;
then you can access it as
AM{i};
note the use of {} to access each element of the cell array AM, that is in turn a matrix

Does MATLAB support named arguments? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Default Arguments in Matlab
How to deal with name/value pairs of function arguments in MATLAB
What if you need to create a function with tones of optional parameters. Is there a way to call that functions later on naming passed arguments for readability:
foo(123, and=456)
The comparable way of doing this in Matlab is to have name/value pairs:
foo(123,'and',456,'something',[1 2 3])
See the answers to this question on how to deal with them.