I try to write a code in modelica. I calculate a value for 4 different areas, which are cpm_links for[1:3], cpm_oben[4:6] for, cpm_rechts for[7:9] , cpm_oben for[10:12]. Then I want to store these 4 values in a one variable which is cpm for[1:12]. How should I write a code so that all these 4 variables will be stored in cpm?
cpm_links[1:n_Kapazitaten]=cp_Kunststoff*m_link[1:n_Kapazitaten];
cpm_oben[1:n_Kapazitaten]=cp_Kunststoff*m_oben[1:n_Kapazitaten];
cpm_rechts[1:n_Kapazitaten]=cp_Kunststoff*m_link[1:n_Kapazitaten];
cpm_unten[1:n_Kapazitaten]=cp_Kunststoff*m_oben[1:n_Kapazitaten];
Use the cat function to combine multiple arrays into one. See recent Stack Overflow question How to insert arguments of a vector inside another vector?
Related
I have a script Function.m such that for example, when I write TEST=Function(1,2), I have TEST.x1=4 and TEST.x2=[5,6,7]. I want to use fsolve to help me find input. To be precise, I want to define a function, say a=#(y)Function(1,y)-4 so that when I use [z,vector]=fsolve(#(y)a(y),5), matlab can help me to obtain z=2 and vector=[5,6,7].
I would like to solve it by defining the same structure New_Function.m as Function.m such that it returns x1 values, i.e., TEST=New_Function(1,2) gives TEST=4 only. Then I write new_a=#(y)New_Function(1,y)-4 and solve z=fsolve(#(y)new_a(y),5) and define new_vector=Function(1,z) so that I can access new_vector.x2.
I want to know if it is possible to do my task without defining a new script or amending the content in the existing script. How to write code?
Since Matlab does not allow further referencing the result of a function call, you may need to help yourself with getfield. In your example (provided I got it right), it would be something like New_Func = #(y) getfield(Function(1,y),'x1'). This would take one scalar and return one scalar, i.e., New_Func(y) gives the field value of the struct returned by Function(1,y) associated to the field x1.
In matlab, I stored every line I plotted like so:
app.cplxPlot(app.cplxPlotNumber)= plot(app.UIAxes[app.var.cplx(1);app.var.cplx(1)],[50;250], 'g');
To make things generic, it is as if I did this:
cplxPlot(location)=plot(app.UIAxes,[cplxPlot(1);cplxPlot(1)],[50;250],'g');
This basically stores a bunch of vertical lines. I want to now delete the line with a certain x value. In the command line of matlab, I did this simple if loop to see if my first plot has the x value of 20, and it worked.
if length(find(cplx1Plot(1).XData==value,1,'first'))==1
disp('wow')
end
But, in app designer, the exact same code :
if length(find(app.cplx1Plot(1).XData==value,1,'first'))==1
app.Label.Text='wow'
end
would display the error: "Dot indexing is not supported for variables of this type." Can someone tell me what I am doing wrong? It worked perfectly in a matlab script, but not on app designer and I am not sure why.
Follow up: I just used the class function, and apparently my vector is storing doubles in app-designer, but it stores class in regular matlab... I have no idea why the elements in the vector would ever be a double.
Follow up 2.0: Apparently, if I access a property to store a line, it is stored as a double, but if I choose to make it a local variable instead, it would be a line. I need this to be a property/global variable and don't get why in the world it would ever be stored as a double...
For instances, bob stores doubles in this scenario :
bob(1)= plot([.005;.005],[100;200],'g')
app.Label.Text=string(class(bob(1)));
But, bob stores doubles in this:
app.bob(1)= plot([.005;.005],[100;200],'g')
app.Label.Text=string(class(app.bob(1)));
For context, I declared bob in my properties like this:
bob
I ran a quick test to see how if deals with multiple input values. It appears that the default behavior is to apply and to the collection of values, but I couldn't find any documentation. Can anyone confirm or provide a counterexample?
>> if([1,1,1]) disp(sprintf('hi'));end;
hi
>> if([1]) disp(sprintf('hi'));end;
hi
>> if([1,1,0]) disp(sprintf('hi'));end;
EDIT: to clarify, I don't intend to try to use this "feature," but wanted to be sure I knew how erroneous input would be handled. Suppose, for example, your code read
if(my_function) and the (badly written) my_function usually returns a single value but occasionally returns multiple values. Good practice, of course , would parse the returned values appropriately and feed a single value to if.
I don't find any practical reason to create an if statement which depends on anything but a scalar.
Regarding what's done in MATLAB practically?
You may assume MATLAB applies the function all on the input of the if statement.
This will hold as intuition given the array is non empty.
For example, if we have array - array4Example which is not empty, the statement if on the array - if(array4Example) is equivalent of the statement if on the scalar - if(all(array4Example(:))).
This matches the documentation if the if function.
I'm using MATLAB (Mapping Toolbox) to create a large number of lines between different countries. Since there are so many lines, I'm trying to do this using object-oriented programming. This is the method I've written:
function transline = createlines(transline, Name, base.CapTr.val(a,b), base.EtrOut.val(:,a,b,1), base.EtrOut.val(:,b,a,1), base.EtrIn.val(:,a,b,1), base.EtrIn.val(:,b,a,1), coords(c,2), coords(c,1), coords(d,2), coords(d,1))
where base is a struct and I intend to substitute different values for 'a' and 'b'. e.g., base.CapTr.val(3,4) and create new objects.
The problem is I can't include base.CapTr.val(a,b) and the subsequent entries as inputs to the function because MATLAB declares the '.' to be an unexpected operator.
So, can you please tell me how I can add varying values of base.CapTr.val(a,b), etc. as inputs to the function?
I don't know if I've explained my problem properly, but I hope it's clear.
Thank you.
Is there an idiomatic way in Matlab to bind the value of an expression to the nth return value of another expression?
For example, say I want an array of indices corresponding to the maximum value of a number of vectors stored in a cell array. I can do that by
function I = max_index(varargin)
[~,I]=max(varargin{:});
cellfun(#max_index, my_data);
But this requires one to define a function (max_index) specific for each case one wants to select a particular return value in an expression. I can of course define a generic function that does what I want:
function y = nth_return(n,fun,varargin)
[vals{1:n}] = fun(varargin{:});
y = vals{n};
And call it like:
cellfun(#(x) nth_return(2,#max,x), my_data)
Adding such functions, however, makes code snippets less portable and harder to understand. Is there an idiomatic to achieve the same result without having to rely on the custom nth_return function?
This is as far as I know not possible in another way as with the solutions you mention. So just use the syntax:
[~,I]=max(var);
Or indeed create an extra function. But I would also suggest against this. Just write the extra line of code, in case you want to use the output in another function. I found two earlier questions on stackoverflow, which adress the same topic, and seem to confirm that this is not possible.
Skipping outputs with anonymous function in MATLAB
How to elegantly ignore some return values of a MATLAB function?
The reason why the ~ operator was added to MATLAB some versions ago was to prevent you from saving variables you do not need. If there would be a syntax like the one you are searching for, this would not have been necessary.