Why I get error :Cannot find 'get' method for matlab.gra​phics.inte​rnal.Graph​icsMetaPro​perty class?

7 次查看(过去 30 天)
Hello there
My code is:
function plot_kx2_slider()
% Create a figure with a slider and a plot
f = figure;
k=1
slider = uicontrol('Style', 'slider', 'Units', 'normalized', ...
'Position', [0.1, 0.9, 0.8, 0.05], ...
'Value', 1, 'Min', -5, 'Max', 5);
axes_handle = axes('Position', [0.1, 0.1, 0.8, 0.7]);
% Create a function to update the plot based on the slider value
function update_plot(source, eventdata)
% Get slider value using get function
k = get(source, 'Value');
x = linspace(-5, 5, 100);
y = k * x.^2;
plot(axes_handle, x, y);
title(['f(x) = ', num2str(k), 'x^2']);
xlabel('x');
ylabel('f(x)');
end
% Set the initial plot
update_plot(slider, [0]);
% Add a listener to the slider to update the plot when the slider value changes
addlistener(slider, 'Value', 'PostSet', @update_plot);
end
this code should plot the function f(x)=kx^2 but MATLAB shows me these errors:
-------------------------
Warning: Error executing listener callback for PostSet event on Value dynamic property in object of
matlab.ui.control.UIControl class:
Error using get
Cannot find 'get' method for matlab.graphics.internal.GraphicsMetaProperty class.
Error in plot_kx2_slider/update_plot (line 13)
k = get(source, 'Value');
-------------------------
guys can you help me?

回答(1 个)

Umar
Umar 2024-9-16

Hi @Abdulkarim bohoms,

You mentioned, “this code should plot the function f(x)=kx^2 but MATLAB shows me these errors: ------------------------- Warning: Error executing listener callback for PostSet event on Value dynamic property in object of matlab.ui.control.UIControl class: Error using get Cannot find 'get' method for matlab.graphics.internal.GraphicsMetaProperty class.Error in plot_kx2_slider/update_plot (line 13) k = get(source, 'Value'); ------------------------- guys can you help me?”

Please see my response to your comments below.

First, try to understand the error message:

Error using get Cannot find 'get' method for matlab.graphics.internal.GraphicsMetaProperty class.

This error indicates that the get function is not being used correctly on the slider object. In MATLAB, the get function is typically used to retrieve properties of graphics objects, but in this case, the slider's value should be accessed differently. So, what you need to do is modify the way you retrieve the slider value in the update_plot function. Instead of using get(source, Value), you can directly access the Value property of the slider object. Here’s the corrected version of your code:

function plot_kx2_slider()
  % Create a figure with a slider and a plot
  f = figure;
  k = 1; % Initial value for k
  slider = uicontrol('Style', 'slider', 'Units', 'normalized', ...
      'Position', [0.1, 0.9, 0.8, 0.05], ...
      'Value', 1, 'Min', -5, 'Max', 5);
  axes_handle = axes('Position', [0.1, 0.1, 0.8, 0.7]);
    % Create a function to update the plot based on the slider value
    function update_plot(~, ~)
        % Get slider value directly from the slider object
        k = slider.Value; % Accessing the Value property directly
        x = linspace(-5, 5, 100);
        y = k * x.^2;
        plot(axes_handle, x, y);
        title(['f(x) = ', num2str(k), 'x^2']);
        xlabel('x');
        ylabel('f(x)');
      end
    % Set the initial plot
    update_plot([], []);
    % Add a listener to the slider to update the plot when the slider value 
    changes
    addlistener(slider, 'Value', 'PostSet', @update_plot);
  end

So, if you look at the above code,

the line k = get(source, 'Value'); was replaced with k = slider.Value;

This change directly accesses the Value property of the slider, which is more straightforward and avoids the error. Also, the parameters of the update_plot function were changed to (~, ~) to indicate that the input arguments are not used. This is a common practice when the function does not require the event data. Finally, the initial call to update_plot was modified to pass empty arrays for the parameters, which aligns with the new function signature. So, with these modifications, your MATLAB function should now work correctly, allowing you to interactively adjust the value of ( k ) using the slider and see the corresponding plot of ( f(x) = kx^2 ).

Please see attached.

If you encounter any further issues or have additional questions, feel free to ask!

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by