Clear Filters
Clear Filters

"Invalid use of operator." doesn't go even after trying various solutions :(

2 views (last 30 days)
Hello,
I have written this function in matlab:
function[magnitude, phase]= cartesiantopolar(z1,z2)
for (z1~=0)&& (z2~=0)
% check for errors
if isempty(z1)||isempty(z2)
return
else
mag=sqrt(z1^2 + z2^2)
pha=atan(z2/z1)
magnitude(mag(1,:))
phase(pha(1,:))
%polar=coefficient_of_polar*exp(winkel*i)
end
end
end
I have been receivin this error for the "for (z1~=0)&& (z2~=0)" line:
Invalid use of operator.
I have already read mukltiple posts and tried multiple possibles olutions but none seem to work, does someone perhaps have any ideas on how to overcome this specific error?
I'd really appreciaste any ideas or input of any kind:)
  3 Comments
Smit
Smit on 2 Nov 2023
z1 and z2 are supposed to be inputs given by the user, and the for loop would perform some operations on these inputs if ando nly if one of the two are not 0, hence the use of a for... or... not equal to 0 condition
Stephen23
Stephen23 on 2 Nov 2023
"I have already read mukltiple posts and tried multiple possibles olutions..."
Did any those solutions include reading the FOR documentation?
z1 and z2 are supposed to be inputs given by the user, and the for loop would perform some operations on these inputs if ando nly if one of the two are not 0, hence the use of a for... or... not equal to 0 condition"
That does not explain why you need a loop.
From what you explain, an IF would suffice. Or if you need to handle array inputs, some logical indexing.

Sign in to comment.

Answers (2)

Alan Stevens
Alan Stevens on 2 Nov 2023
Why nor just use Matlab's inbuilt cart2pol function?
  1 Comment
Smit
Smit on 2 Nov 2023
thhe task was to write a function that performs the same task, which is what we tried to do, for any given input z1 and z2

Sign in to comment.


Steven Lord
Steven Lord on 2 Nov 2023
That doesn't match any of the allowed syntaxes shown on the for keyword documentation page.
If you want the code to run only if all elements of z1 and z2 are non-zero, you want to use the if keyword instead. [Note the description of how if handles non-scalar conditions. Depending on what you want to do you may need to use the any or all functions around your condition.]
If you wanted it to do something else, please explain in more detail (in words not code) what you're trying to achieve and we may be able to suggest the right way to write that code.
  2 Comments
Smit
Smit on 2 Nov 2023
We want to be able to insert as many numbers in to our function as selected by the user starting at many as 2 numbers. And at the end give the numbers out of the function devided into two factors which are then given into a second function which divdes them into seperates numbers again and runs a conversion of the numbers.
Steven Lord
Steven Lord on 2 Nov 2023
So what do you want the elements of magnitude and phase whose corresponding elements in x1 and/or x2 are 0 to contain? So what if I had the following?
x1 = [0 1 0 -1 0 0 1];
x2 = [1 0 -1 0 1 0 1];

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!