How to: Update/Refresh Graph / Axes
Show older comments
Hello,
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?
Accepted Answer
More Answers (7)
Jan
on 27 Dec 2012
0 votes
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.
Image Analyst
on 28 Dec 2012
You can call Brett Shoelson's deleteoutliers http://www.mathworks.com/matlabcentral/fileexchange/3961 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]);
Shaun VanWeelden
on 28 Dec 2012
0 votes
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.
Hello kity
on 28 Dec 2012
0 votes
Hello kity
on 28 Dec 2012
1 Comment
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.
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;
plot(x);
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);
2 Comments
Hello kity
on 28 Dec 2012
Jan
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.
Hello kity
on 28 Dec 2012
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.
Categories
Find more on Axis Labels 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!