Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit:

How to: Update/Refresh Graph / Axes

Asked by Hello kity on 27 Dec 2012


I programmed a gui which makes a graph whom sometimes has outliers so the axes are too wide. i want to make the option to choose the range.

the question is how to do this the best way? I thought of making two edit boxes, input for min and max x value. then a callback button with handles to the two editboxes.

is there are better way?


Hello kity


No products are associated with this question.

8 Answers

Answer by Hello kity on 28 Dec 2012
Accepted answer

i looked at that. could be useful but just entering the values is sufficient.

i have this code: Minimum1=str2double(get(handles.MIN_Input,'String')); Maximum1=str2double(get(handles.MAX_Input,'String')); xlim([Minimum1 Maximum1]);

at the first run you dont know how the graph looks like so you dont set any limits. but my code runs this and doesnt find any value so it stops.

how to program an if that says only to use this xlim line when both values are entered,

so Minimum1=str2double(get(handles.MIN_Input,'String')); Maximum1=str2double(get(handles.MAX_Input,'String'));

if Minimum1 and Maximum1 is entered
xlim([Minimum1 Maximum1]);
if not then just plot xlim auto 


Jan Simon on 28 Dec 2012

Now you have accepted your own answer, which contains the problem description.

Image Analyst on 28 Dec 2012

I knew he'd do that, even though you told him not to - I was just waiting for it, though I thought he'd accept his "Answer" that's at the bottom of this page instead. Though, I still heard your hand slapping your forehead all the way over here. Incredible, huh? Actually I believe you were secretly expecting that too, but it's still amazing when you get that Nostradamus feeling.

I also have that - lots of times people accept clearly the wrong or worse answer instead of mine, if they even accept any at all. Or sometimes they accept the wrong one by mistake (slip of the mouse?) but there's no way (yet) to correct it to the right answer. Or they accept one too soon and can't "unaccept" it.

Jan Simon on 28 Dec 2012

I've unaccepted some of my answers by deleting and re-posting them. But this was possible when no comments have been posted for this answer. But is most cases the author did not accept another answer afterwards...

Hello kity
Answer by Jan Simon on 27 Dec 2012

Is the outlier a single point? Then it could be more convenient to select it with with mouse and remove it from the displayed data, such that Matlab's automatic limits are applied again.


Jan Simon
Answer by Image Analyst on 28 Dec 2012

You can call Brett Shoelson's deleteoutliers and then find the max and min of the data without the outliers in there, then use ylim to set:

betterY = deleteoutliers(badY,........
minYValue = min(betterY(:));
maxYValue = max(betterY(:));
ylim([minYValue, maxYValue]);


Image Analyst
Answer by Shaun VanWeelden on 28 Dec 2012

Honestly, your initial solution of having two editable text boxes is really a good one, while it may require slightly more work for the user if an outlier pops up, they have complete control of whats being displayed.

I would note though that instead of just adjusting the X range, maybe just do something like plot(orig*(orig<get(max_value) && orig>get(min_value))). Effectively multiplying your data by a logic vector to eliminate data you don't want without changing or making anything.

The above code needs some customization for your specific gui obviously, but I think that would work best. In this way, you aren't changing the data at all, just what you plot.


Shaun VanWeelden
Answer by Hello kity on 28 Dec 2012

It is not a single point. it varies. i want to give the user the option to choose , because now i just have a zoom button but that just zooms and the for example the bar width doesnt change (bar graph).

somehow i need to read those two edit text use that to make new data (data(ouliers) etc. and let matlab make new graph with data w/o outliers.

i ll look in to the text above. thank you


Hello kity
Answer by Hello kity on 28 Dec 2012

i am currently busy with the axes but i fail at handles(reading between figures/m files).

this works function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
     case 1
bar(Minimum1 Maximum 2)

the edit function (callback and createfunc) are in the same m-file.

but i want this: function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

    popup_sel_index = get(handles.popupmenu1, 'Value');
    switch popup_sel_index
         case 1
function Anothermfile

but as you can expect, this doesnt work, it doesnt know where the handles/mininpt edit boxes are.

1 Comment

Jan Simon on 28 Dec 2012

Please format your code properly: One empty line before and after the code, mark the code, hit the "{} code" button. Alternatively insert two leading spaces in each line instead of the last step.

To get the newest handles struct:

handles = guidata(hObject);

The handles struct from the input arguments is the one, which existed during the creation of the GUI and is most likely incomplete. Keeping this incomplete handles struct in the callbacks of GUIs created by GUIDE is one of the most annoying design errors in Matlab. You find hundreds of corresponding questions in this forum.

Hello kity
Answer by Jan Simon on 28 Dec 2012
Edited by Jan Simon on 28 Dec 2012

Another idea:

x = rand(1, 100);
x([4,28,67]) = 98;  % Pseudo outlieres
meanX = mean(x);
stdX  = std(x);
minX  = min(x);
maxX  = max(x);
lowerY = max(minX, meanX - 2 * stdX);  % Or 3 * ...
upperY = min(maxX, meanX + 2 * stdX);
axesH  = axes;
set(axesH, 'YLim', [lowerY, upperY]);

And as usual in Matlab, you could choose the next integer power to 10 also:

% Faster LOG10(x): LOG2(x) / LOG2(10)
rangeY = 10 ^ (round(log2(upperY - lowerY) * 0.3010299956639814) - 1);
limitY = [floor(lowerY / rangeY), ceil(upperY / rangeY)] * rangeY;
limitY = [max(lowerY, limitY(1)), min(upperY, limitY(2))];
set(axesH, 'YLim', limitY);


Hello kity on 28 Dec 2012

this is nice but the data varies so much and i want the user to see the initial outliers. after seeing the data with outliers, the user should have the option to choose the axis.

Jan Simon on 28 Dec 2012

Instead of using mean+-n*std the user can select the range by dragrect also graphically. Afterwards the rounding to the power of 10 might improve the readability.

But do not let such suggestions confuse you. The two edit fields are smart and exact already.

Jan Simon
Answer by Hello kity on 28 Dec 2012

hmm my code was almost similar :) , i am starting to learn this complex programme

                  if (~isnan(Minimum1))&&(~isnan(Maximum1))
                  set(h,'xlim',[Minimum1 Maximum1]);              

if u dont use set all the subplots uses that one xlim.

1 Comment

Image Analyst on 28 Dec 2012

I'm not sure what that last comment means. There is no "set all" command. You can use xlim or ylim (as I originally suggested) to set the range of the axes and it applies to the current axes ONLY, not to all the axes on your GUI. I do it all the time. And you don't have to do it the more "complex" way of using set() and passing it the axes handle like you did, though you can.

Hello kity

Contact us