going from command window (as script) to function - matlab

the program is working perfectly but when i am changing it to function the follwing error is beeing displayed:
[Parent1index, Parent1Position, alldcel] = Parent1n(TotalnoOfGrids, noOfNodes, Penalties, test)
??? Index exceeds matrix dimensions.
Error in ==> Parent1n at 10
[~,index]=min(alldcel{t});

Because alldcell{t} may not exist for some values of t if the condition to assign values to it in
if Penalties{t}(r)== 0;
alldcel{t}(r)=inf;
end
is never satisfied. Assume for some t that all values of Penalties{t} are different than zero. Then you would never assign inf to alldcell{t}. That means, you are only extending the cell array alldcell when Penalties{t} is zero for some r. If the condition is never satisfied, alldcell{t} will not exist and asking for it will give you a cell array error.
You should at least initialize it using alldcell = cell(TotalnoOfGrids,1).
Also, comparing for equality to zero using a==0 is not a good idea. You should use abs(a)<tol for some small value tol.

ok with this code the functiion worked:if Penalties{t}(r)> 0;
alldcel {t}(r)=alldcel{t}(r);
else alldcel {t}(r)=inf; but interchanging if statement with else did not

Related

getting the value from a checkbox in Matlab 2018

I am upgrading my Matlab from 2013b to 2018b and have found out that MathWorks have made quite a few changes to the GUI's.
One problem I am having is getting the value of checkbox. The line below is the code I used to use but now it doesn't work.
if get(handles.check_perf_attr,'Value') == 1
The error message is,
Undefined operator '==' for input arguments of type 'cell'.
So I tried the line below to just get the value that is being returned and then apply some logic.
tValue = get(handles.check_perf_attr,'Value');
However tValue is 2 x 1 cell which in (1, 1) = 0 & (2, 1) = 1. I don't really understand this as surely a checkbox can only be one value true (1) or false (0)?
get returns a cell array with values when applied to an array of handles.
Thus, I think your problem is that handles.check_perf_attr contains two handles, not one.
"Dot notation is a new syntax to access object properties starting in R2014b."
so try
if handles.check_perf_attr.Value == 1
or
tValue = handles.check_perf_attr.Value;

Error using == Matrix dimensions must agree

I've been working on an assignment.
One part of the code I've been working on doesn't work.
There is a file called txt that is a 12x1 cell array with 12 words all in a column, and the code needs to call each name one by one, and do some calculations.
I need the code in line 30 to check if the cell contains the word shmoop, if it does, than it excecutes the code, otherwise it does the other code.
How do I check if the array is the word shmoop!
THANK YOU!
Error using ==
Matrix dimensions must agree.
Error in asd (line 30)
if lanes == 'shmoop';
for c = 2:d
lanes = txt{c,1};
if lanes == 'shmoop';
Monstershit{d,2}=paces;
Monstershit{d,3}=pacez;
Monstershit{d,4}=pacea;
else
Monstershit{d,2}=pacev;
Monstershit{d,3}=peace;
Monstershit{d,4}=pasem;
end
end
Comparing strings with == only works if the dimensions of the two objects are the same. In general, you should use strcmp instead.

Error using minus in MATLAB

temp(i,1) = rand(1)*(pb(1,num).pos(i,1) - pw(1,num).pos(i,1));
This line gives the following error:
Error using ==> minus
Not enough input arguments.
The following are the definitions of pb and pw.
pw=struct('fitness',[],'pos',{});
pb=struct('fitness',[],'pos',{});
pos is a 2 x 1 array.
When tracking down errors like this, I break the problem up into smaller bits. Especially when the logic isn't readily apparent. Not only does it provide a path that can be used to step through your function using the debugger, but it also makes it more readable.
I've taken liberty with the intermediate variable names.
thisPb = pb(1,num);
thisPw = pw(1,num);
initialPos= pw.pos(i,1);
finalPos = pb.pos(i,1);
whos initialPos finalPos
temp(i,1) = rand(1) * (finalPos - initialPos);
The line with whos will print out the values. Make sure that finalPos and initialPos are both numbers.
One way that you can get this error is when num is an empty matrix.
The expression
>> s(x).a
can return a variable number of outputs, depending on the size of x.
If x = [1,2,3] for example, it will return three values (as long as s has at least three elements).
If x = [] on the other hand, then s(x).a will return no outputs, so the expression
>> disp(s(x).a)
will give you a Not enough input arguments error, which is almost certainly what you're seeing. You should check that num is not empty.
Are you sure, that all values are really initialised? Try to check this before your codeline.
disp(pb(1,num).pos(i,1))
disp(pw(1,num).pos(i,1))
temp(i,1) = rand(1)*(pb(1,num).pos(i,1) - pw(1,num).pos(i,1));

matlab - what is the equivalent of null / None / nil / NULL etc.?

In most OO languages, where variables may point to objects, they may also have a null value, which is highly convenient.
In Matlab, I have a function which parses a command, and then returns a cell array, or false (which is equal to zero — which is another common pattern) if it fails:
function re = parse(s)
...
if (invalid)
re = false;
return;
end
end
The problem is that when I check the result, it gives an error:
re = parse(s);
if (false == re)
Undefined function 'eq' for input arguments of type 'cell'.
I've written a function to check it without an error: strcmp('logical', class(re)) && false == re, but that seems to be really slow for use in hot areas of the code, and also inconvenient if I have to add this function to every M file I'm writing.
Using NaN is even worse, because besides throwing that error, it also isn't equal to itself.
What's a better alternative for use with this pattern?
You can use the isequal function to compare any two items without causing that error. For example:
if isequal (re, false)
%code here
end
A good alternative is to use the empty array: [] and isempty(re) to check. This doesn't throw the error.
Reference: http://www.mathworks.com.au/matlabcentral/newsreader/view_thread/148764
If you can change the function parse one solution would be to return two output arguments [re status] = parse(s), where status would be logical variable. Set it to true in case of success, and to false otherwise.
I would use the empty cell array {} if it is not a valid result otherwise. Using empty matrices is MATLAB standard (see Evgeni Sergeev's answer), but using an empty cell array instead of an empty numeric array ensures that you'll always end up with the same type of result.
If, on the other hand, the empty cell array {} is a valid result of your function, then I'd use an exception to signalize a problem:
if invalid
error('Parse:InvalidArgumentError', 'The input is invalid.');
end
Make sure to use an appropriate error ID (first argument to error) so that you can catch exactly that exception when you call the function:
try:
result = parse(something);
catch ME
if strcmp(ME.identifier, 'Parse:InvalidArgumentError')
fprintf('Ooops\n');
else
% Some other error
ME.rethrow();
end
end
I think the problem is that matlab functions don't return pointers but copies of values.
IMHO the best best approach would be to define your own "pointer" class. Inside you can define an "isNull()" command or even override comparison to produce the behavior you desire.

Using loop in fsolve and plugging the result to a function

I have functions which has totally 4 unknowns (2x2).
I got the solution. But what I am going to do is to vary some parameters so that I can see how the original solution changes.
But matlab keeps saying that in A(I)=B, 'I' and the number of element B must be the same.
Example code could be such as( in the code, psi01 psi02 psi03 psi04 are the varying parameters);
R=902;
psi01=0.9:-0.1:0.1;
psi11=0.9:-0.1:0.1;
psi02=0.1:0.1:0.9;
psi12=0.1:0.1:0.9;
E0=[R/4 R/4; R/4 R/4];
for i=1:9
vv=#(E) [
g/E(2,1)-(th1+psi12(i)/psi11(i)*th2)-((psi01(i)/psi11(i))+a*b)/b*(g/E(1,1)-(rho*b* (psi11(i)/psi01(i))/(psi01(i)/psi11(i)+a*b))*(th1+psi12(i)/psi11(i)*th2)*(th1+psi02(i) /psi11(i)*th2) );
g/E(2,2)-(psi11(i)/psi12(i)*th1+th2)-((psi02(i)/psi12(i))+a*b)/b*(g/E(1,2)-(rho*b*(psi12(i)/psi02(i))/(psi02(i)/psi12(i)+a*b))*(psi11(i)/psi12(i)*th1+th2)*(psi01(i)/psi02(i)*th1+th2) );
R-(E(1,1)+E(1,2)+E(1,1)+E(2,2));
E(1,2)- c(E)*E(1,1);
E(2,2)- d(E)*E(2,1)
];
Ep2(i)=fsolve(vv, E0);
end
If the result of fsolve is a 2x2 array, then how can you expect to save it as a scalar?
Ep2(i)=fsolve(vv, E0);
Ep2 is being used here to store a single element, a scalar. You can't stuff 4 elements into one, and not get an error.
Use a 3-dimensional array, a struct, or use a cell array.