Error with Set function when I am trying to change the Xlabel form a figure (Unable to use a value of type matlab.ui.Figure as an index.)

12 views (last 30 days)
I am getting an error in the Set function. I have been trying to solve the problem but havent been able to get through. In plamce of 'figure(1)', I also tried gca but it showed the same error. (Unable to use a value of type matlab.ui.Figure as an index.)

Accepted Answer

Guillaume
Guillaume on 11 Mar 2020
Rather than giving us a screenshot of the code, which we can't copy and edit, paste your code directly in your question. Also it would be very useful to have the full text of the error message, not your interpretation of it.
Rather than using gcf or gca get the figure handle when your create it:
hfig = figure(1);
%... then use hfig everywhere you used gcf or fig
Further on, your code attempts to set the 'XScale' property on a figure but XScale is not a figure property. Possibly you meant
set(hfig.CurrentAxes, 'XScale', 'linear', 'YScale', 'linear');
which you could also write as:
hfig.CurrentAxes.XScale = 'linear';
hfig.CurrentAxes.YScale = 'linear';

More Answers (2)

Steven Lord
Steven Lord on 11 Mar 2020
Rename the variable you've created in your workspace named set.
You intended to call the set function included in MATLAB on the line "set(figure(1), 'XScale', 'linear', 'YScale', 'log')" but because the variable exists MATLAB interpreted that as an attempt to index into the variable. MATLAB doesn't know how to use a figure handle as an index into a variable so it throws an error.
You should also use a handle to an axes instead of a figure to set the XScale and YScale properties since those are axes properties not figure properties, as tmarske and Guillaume stated, but you need to remove the variable named set as well.

tmarske
tmarske on 11 Mar 2020
XScale and ySCale are properties of the axes, not the figure, so you need to pass an axes handle in as the first argument to set(). Try:
set(ax, 'XScale', 'linear', 'YScale', 'linear')

Categories

Find more on Data Exploration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!