Error: Data must be numeric, datetime, duration, categorical, or an... (2024)

43 views (last 30 days)

Show older comments

Ammar on 7 Jul 2024 at 22:45

  • Link

    Direct link to this question

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double

  • Link

    Direct link to this question

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double

Commented: Voss on 8 Jul 2024 at 3:19

Accepted Answer: Voss

  • Plotting_Theta_Phi_Function.m

Hello everyone,

I am trying to plot a function that has two different variables: theta and phi.

Theta = -pi/2:0.1:pi/2

Phi = Specific Values at 0 and pi/2

I have an error that says:

"Error: Data must be numeric, datetime, duration, categorical, or an array convertible to double."

I've tried looking up this error on different discussions. However, I wasn't able to find anything that helped with my specific scenario.

I have tried different methods and approaches: Using "symbolic variables", fimplicit function, fplot function, etc. However, I got different errors everytime I tried a different approach.

I attached my code for reference:

Plotting_Theta_Phi_Function.m

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Voss on 7 Jul 2024 at 22:55

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#answer_1482381

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#answer_1482381

Open in MATLAB Online

  • Plotting_Theta_Phi_Function.m

tic

close all; clc;

% Declaring Theta and Phi Variables

theta = 0:0.1:pi/2;

% Phi Values

phi_E_Plane = 0;

phi_H_Plane = pi/2;

phi = [phi_E_Plane, phi_H_Plane];

% Declaring x and y variables as theta and phi

x = sin(phi);

y = cos(theta);

% Function Z is a combination of x and y

Z = @(x,y) x.^2.*y + y.^2.*x;

% Calculating E-Plane and H-Plane "values"

E_Plane = Z(theta,phi(1));

H_Plane = Z(theta,phi(2));

% Plotting E-Plane = Theta Range at phi = 0

plot(theta,E_Plane);

% Holding on to put the H-Plane on the same grid as the E-Plane

hold on;

plot(theta,H_Plane);

% Placing a grid on the graph

grid on;

% Title, Axes, and Legends

title('E-Plane (dB) and H-Plane (dB) versus Theta (Radians)');

legend({'E-Plane','H-Plane'},'Location','northwest');

xlabel('Theta (Radians)');

ylabel('Magnitude (dB)');

Error: Data must be numeric, datetime, duration, categorical, or an... (3)

toc

Elapsed time is 0.343450 seconds.

5 Comments

Show 3 older commentsHide 3 older comments

Ammar on 7 Jul 2024 at 23:28

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205146

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205146

Edited: Ammar on 7 Jul 2024 at 23:51

Open in MATLAB Online

Hello Voss,

Thank you for the quick response.

My main concern is that my function "Z" is going to grow larger and larger. In other words, its going to be comprised of many different variables and functions that include phi and theta. At that point, I won't be able to "directly" write out the function for "Z".

For example:

x = sin(theta)

y = cos(phi)

a = sin(2*theta)

Y = (a).^2 + (y).^2 + (x).^2

R = 2*Y + (a).^2

Z = (R).^2 + (Y).^2

That's why I used the function "F" to say:

F = @(theta,phi) (Z)

Is there another method to plot "Z" for the specific cases that I have? Thanks.

Walter Roberson on 8 Jul 2024 at 1:15

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205176

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205176

Open in MATLAB Online

F = @(theta,phi) (Z)

means that F is to be defined as an anonymous function that accepts up to two parameters. When invoked, the anonymous function will ignore the two parameters, and will return whatever is stored in Z.

But what is stored in Z? Well, you do several calculations on numeric objects, to calculate Z. So Z is a numeric object.

... Except that chances are that theta and phi are undefined at that point, and x = sin(theta) is likely to fail.

You have two choices:

function Z = F(theta, phi)

x = sin(theta);

y = cos(phi);

a = sin(2*theta);

Y = (a).^2 + (y).^2 + (x).^2;

R = 2*Y + (a).^2;

Z = (R).^2 + (Y).^2;

end

or

x = @(theta,phi) sin(theta);

y = @(theta,phi) cos(phi);

a = @(theta,phi) sin(2*theta);

Y = @(theta,phi) a(theta,phi).^2 + y(theta,phi).^2 + x(theta,phi).^2;

R = @(theta,phi) 2*Y(theta,phi) + a(theta,phi).^2;

Z = @(theta,phi) R(theta,phi).^2 + Y(theta,phi).^2;

F = Z;

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205186

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205186

Edited: Voss on 8 Jul 2024 at 1:33

It may be convenient if you write a named function that calculates the value(s) of Z, given value(s) of phi and theta and whatever else Z depends on. To use your example:

function Z = getZ(theta,phi)x = sin(theta);y = cos(phi);a = sin(2*theta);Y = a.^2 + y.^2 + x.^2;R = 2*Y + a.^2;Z = R.^2 + Y.^2;end

Which is the same (except for the name of the function) as the first option suggested by Walter.

Ammar on 8 Jul 2024 at 2:54

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205236

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205236

Open in MATLAB Online

Hi Voss and Walter,

Thank you for providing this guidance. As far as everything else (plotting, setting the step size for theta, etc), is that meant to be done within the function or outside?

I understand how to "execute the function" F(theta,phi) value. However, I'm trying to extract and then plot specific values for "F".

Would I need to write "seperate" functions for each "task" I'm trying to do: plotting, setting grid size, axes limits, etc? Or can I just write everything into one function?

theta = -pi/2:0.01:pi/2;

phi = [0 pi/2];

E_Plane = F(theta,0);

H_Plane = F(theta,pi/2)

% Then plotting the E-plane and H-plane

plot(theta,E_Plane)

hold on;

plot(theta,H_Plane)

% Other Axis and Grid Settings....

% etc...

Voss on 8 Jul 2024 at 3:19

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205251

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205251

The function F (or getZ as I called it) would only calculate Z given theta and phi. Everything else would be outside the function, either in a script or in separate functions if you prefer.

Sign in to comment.

More Answers (1)

Torsten on 7 Jul 2024 at 22:57

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#answer_1482386

  • Link

    Direct link to this answer

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#answer_1482386

Open in MATLAB Online

% Declaring Theta and Phi Variables

theta = 0:0.1:pi/2;

% Phi Values

phi_E_Plane = 0;

phi_H_Plane = pi/2;

% Declaring x and y variables as theta and phi

x = @(phi)sin(phi);

y = @(theta)cos(theta);

% Declaring the function Z as a function of theta and phi

F = @(theta,phi)x(phi).^2.*y(theta) + y(theta).^2.*x(phi);

% Calculating E-Plane and H-Plane "values"

E_Plane = F(theta,phi_E_Plane);

H_Plane = F(theta,phi_H_Plane);

% Plotting E-Plane = Theta Range at phi = 0

plot(theta,E_Plane);

% Holding on to put the H-Plane on the same grid as the E-Plane

hold on;

% Plotting H-Plane = Theta Range at phi = pi/2

plot(theta,H_Plane);

% Placing a grid on the graph

grid on;

% Title, Axes, and Legends

title('E-Plane (dB) and H-Plane (dB) versus Theta (Radians)');

legend({'E-Plane','H-Plane'},'Location','northwest');

xlabel('Theta (Radians)');

ylabel('Magnitude (dB)');

Error: Data must be numeric, datetime, duration, categorical, or an... (10)

1 Comment

Show -1 older commentsHide -1 older comments

Ammar on 7 Jul 2024 at 23:51

Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205151

  • Link

    Direct link to this comment

    https://uk.mathworks.com/matlabcentral/answers/2135276-error-data-must-be-numeric-datetime-duration-categorical-or-an-array-convertible-to-double#comment_3205151

Edited: Walter Roberson on 8 Jul 2024 at 1:08

Open in MATLAB Online

Hello Torsten,

Thank you for the quick response.

My main concern is that my function "Z" is going to grow larger and larger. In other words, its going to be comprised of many different variables and functions that include phi and theta. At that point, I won't be able to "directly" write out the function for "Z".

For example:

x = sin(theta)

y = cos(phi)

a = sin(2*theta)

Y = (a).^2 + (y).^2 + (x).^2

R = 2*Y + (a).^2

Z = (R).^2 + (Y).^2

That's why I used the function "F" to say:

F = @(theta,phi) (Z)

Is there another method to plot "Z" for the specific cases that I have? Thanks.

Sign in to comment.

Sign in to answer this question.

See Also

Tags

  • plotting
  • functions

Products

  • Symbolic Math Toolbox

Release

R2024a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Error: Data must be numeric, datetime, duration, categorical, or an... (12)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Error: Data must be numeric, datetime, duration, categorical, or an... (2024)
Top Articles
Steve Hytner Net Worth
OSRS Sand Crabs Guide - How To Get There + Training Methods - OSRS Guide
Joliet Patch Arrests Today
Ups Stores Near
Tesla Supercharger La Crosse Photos
Wmu Course Offerings
Arrests reported by Yuba County Sheriff
Aiken County government, school officials promote penny tax in North Augusta
Jasmine
City Of Spokane Code Enforcement
William Spencer Funeral Home Portland Indiana
Elle Daily Horoscope Virgo
Yesteryear Autos Slang
The Binding of Isaac
Explore Top Free Tattoo Fonts: Style Your Ink Perfectly! 🖌️
978-0137606801
Saberhealth Time Track
Chic Lash Boutique Highland Village
Sivir Urf Runes
Bcbs Prefix List Phone Numbers
Connect U Of M Dearborn
Dirt Removal in Burnet, TX ~ Instant Upfront Pricing
Missouri Highway Patrol Crash
91 East Freeway Accident Today 2022
Strange World Showtimes Near Roxy Stadium 14
Full Standard Operating Guideline Manual | Springfield, MO
Dover Nh Power Outage
Best Nail Salons Open Near Me
Mega Personal St Louis
Atlases, Cartography, Asia (Collection Dr. Dupuis), Arch…
MyCase Pricing | Start Your 10-Day Free Trial Today
F45 Training O'fallon Il Photos
Surplus property Definition: 397 Samples | Law Insider
Bento - A link in bio, but rich and beautiful.
Keyn Car Shows
Vera Bradley Factory Outlet Sunbury Products
Free T33N Leaks
Jail Roster Independence Ks
Till The End Of The Moon Ep 13 Eng Sub
Calvin Coolidge: Life in Brief | Miller Center
Ff14 Laws Order
How To Make Infinity On Calculator
Today's Final Jeopardy Clue
Dallas City Council Agenda
Let's co-sleep on it: How I became the mom I swore I'd never be
Engr 2300 Osu
Bustednewspaper.com Rockbridge County Va
Fairbanks Auto Repair - University Chevron
Denise Monello Obituary
Joblink Maine
A Snowy Day In Oakland Showtimes Near Maya Pittsburg Cinemas
Predator revo radial owners
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 6421

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.