If else for class of data

9 次查看(过去 30 天)
Hi all,
I have been trying to create a function which plots two inputs against each other and their derivatives against eachother on the same plot. I indend to use this function with data from a timetable. I want to be able to use a datetime input or a double in my xdata, so when xdata is a double it simply plots dx against dy, or when xdata is a datetime variable xdata is plotted against dy. Below is my function;
function createFig2(xdata,ydata) %xdata is either double or datetime, ydata is double
plot(xdata,ydata,'-k') %plot inputs
hold on
if class(xdata) == char('double') %check if xdata is double
dx = diff(xdata);
dy = diff(ydata);
plot(dx,dy,'--r')
else %if xdata is datetime
dy = diff(ydata);
plot(xdata,dy,'--r') %plot xdata against dy
end
hold off
xlabel('xdata')
ylabel('ydata')
end
When I attempt to run this function I get an error in Line 4;
"Matrix dimensions must agree.
Error in createFig2 (line 4)
if class(xdata) == char('double')".
I am not very familiar with ifelse statements or logically statements in matlab so I'm not sure how to fix this. Thank you in advance for your help! :)

采纳的回答

Walter Roberson
Walter Roberson 2020-6-20
if isa(xdata, 'double')
Or
if strcmp(class(xdata), 'double')
or
if class(xdata) == "double" %notice this is not 'double' but "double"
  3 个评论
Cameron
Cameron 2023-7-13
Another option
if class(xdata) == string('double')
Walter Roberson
Walter Roberson 2023-7-14
string('double') is less efficient than "double", but was needed for the very first release that supported string datatype

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by