How can I display an error message in MATLAB? - matlab

I was doing a model for a slider-crank mechanism and I wanted to display an error for when the crank's length exceeds that of the slider arm. With the crank's length as r2 and the slider's as r3, my code went like this:
if r3=<r2
error('The crank's length cannot exceed that of the slider')
end
I get the error:
??? error('The crank's length cannot exceed that of the slider')
|
Error: Unexpected MATLAB expression.
can someone tell me what I'm doing wrong and how to fix it please?

When you want to use the ' character in a string, you have to precede it with another ' (note the example in the documentation):
if (r3 <= r2)
error('The crank''s length cannot exceed that of the slider');
end
Also, note the change I made from =< to <=.

You can print to the error handle as well:
fprintf(2,'The crank''s length cannot exceed that of the slider');

I believe the comparison operator should be <= not the other way around, unless that was only a typo in your question
Also you should escape the ' character using ''

Related

“Index exceeds the number of array elements” for piecewise function

I am trying to create piecewise functions V(t), D(t).
I try to find the piecewise, then I use the piecewise of t to construct functions and plot them.
But it shows "Index exceeds the number of array elements. Index must not exceed 51".
How can I fix it?
I put my code below and I really hope someone can answer it. Thaks!
z=zeros(1,50);
p_i=zeros(1);
p=0.023;
for i=1:50
z(i)=rand;
if z(i)>p
p_i(end+1)=i+z(i);
end
end
n=numel(p_i);
V=zeros(1,n);
w=zeros(1,n);
D=zeros(1,n);
V_op=zeros(1,n);
%get the number of pi
sigma_w0=0.2;
Q=5;
P=2;
Q_op=4;
for i=1:n
if i>1
w(i)=w(i-1)+normrnd(0,sigma_w0);
V(i)=Q*w(i);
D(i)=P*w(i);
V_op(i)=Q_op*w(i);
else
w(1)=2;
V(i)=Q*w(i);
D(i)=P*w(i);
V_op(i)=Q_op*w(i);
end
end
t=0:0.0002:50;
V_p=zeros(size(t));
D_p=zeros(size(t));
V_opp=zeros(size(t));
for m=1:length(t)
t(m)>=p_i(i)& t(m)<p_i(i+1)
V_p(m)=V(i);
D_p(m)=D(i);
V_opp(m)=V_op(i);
end
Yes, if you run your code you'll see it is functional before the last for loop since it is evaluating that in the 1 to 50 range (m=1:length(t)) but your line is printing 51 values so you need to check only the next part and reorganize the idea:
t(m)>=p_i(i)& t(m)<p_i(i+1)
If you print the first part (t(m)>=p_i(i)) it is okay, but check the other part and you'll notice the error. Maybe you can print all your results moving your increment value (+1) and prevent it from exceeding 1 to 51.

Check whether a given number is a power of two

I am trying to write a function which returns a value 1 if the number entered is a power of two, else it returns a value 0.
function val = func_1(num)
while not(num == 1)
if num%2~=0
val=0;
break
end
num=num/2;
val=1;
end
end
But unfortunately, the function always return a value 1. Is there any error in the algorithm or the code? Thanks in advance for any help.
In Matlab, % is the comment character. Everything from that point onwards until the end of the line is ignored. This means that this line if num%2~=0 is not doing what you think it does.
Instead, use the mod function. link. In this case, the line in question becomes if mod(num, 2)~=0.
On a separate note, I suspect there is a much more efficient way of doing this. See, for example, here.

Parse error at '=': usage might be invalid Matlab syntax

Why can't I do this loop this way ?
for i=1:N
(Teta(2,i)-Teta(1,i))/dY=0;
end
I am getting this error : Parse error at '=': usage might be invalid Matlab syntax
You cannot make calculations in the left side of the equals sign. Calculations can only be made in the right-hand side.
For example, like this:
for i=1:N
Teta(2,i) = 0 * dY + Teta(1,i);
end
Without knowing anything about the application of you problem, I just moved everything but the Teta(2,i) from the left to the right. This will not give that error. But probably isn't the result you wish wither...

reshape() command causes an error being evoked from a loop

psz=length(pic)
p=0; %masking counter
for i=1:outs:(psz) % dividing in blocks
for j=1:outs:(psz)
p=p+1
blocks(:,:,p)=pic(i:i+outs-1,j:j+outs-1);
ins(:,p)=reshape(blocks(:,:,p)',1,ins')';
end
end
so to begin with i am trying to reproduce sanger rule for pca using neural networks so if somone wants to duscuss about it or give him my code he can message me:)
i get the following error
Error using reshape
To RESHAPE the number of elements must not change.
Error in train (line 30)
ins(:,p)=reshape(blocks(:,:,p)',1,ins')';
Converting blocks(:,:,p) to a column vector should clear the error as long as there are the same number of elements in blocks(:,:,p) as the row length of ins
col_vec = blocks(:,:,p);
ins(:,p) = col_vec(:);
The size of blocks(:,:,p) is outs-by-outs so to make a column vector, it has to be (outs*outs)-by-1. To do this, the command would be:
ins(:,p)=reshape(blocks(:,:,p)',outs*outs,1); % no need for '
However, be sure that size(ins,1) is outs*outs or it won't work. What is the size of ins (and blocks, out of curiosity)? Also, be sure you really want the ' on blocks because the command will work with or without it.

What does this symbol ~ do in matlab

I am student in physics and they gave me a program in matlab to get some results. Is some point matlab crashes and indicates the problem at this line:
[~,idx] = min(cf(:));
error message is:
Expression or statement is incorrect--possibly unbalanced (, {, or [.
I want to ask what does ~ do in matlab? At my search in google I found that ~ is the approximately symbol. So what reason does it have to be there?
The tilde in that expression is used to ignore the first return value from the min function. That syntax has only been around for a few years, it's possible the error is occurring because you're using an older version of MATLAB.
Try replacing the ~ with idx. That'll cause the second return value to overwrite the first one, and will be functionally equivalent to the code you've posted.
In other contexts, ~ is the logical not operator and ~= is a logical comparison operator for testing inequality.
min function can return two values, where the first is the minimum value of the input array and the second is the index corresponding to the minimum value. Sometimes you don't really need the actual minimum value, so for the first return value you just put ~ there as a place holder, without assigning it to a specific variable.
The error is probably associated with an unbalanced statement in preceding lines of the function call you are showing