MATLAB Answers

carl chew

Rounding to a specific number

Asked by carl chew
on 11 Mar 2013

I am using excel and matlab together. I have a gui interface. One text edit box is used to input any number desired. I would like to take that number and round it to the nearest value that is in my excel spreadsheet. For example, if I input the number 148 and my excel sheet has values 140, 155, 161. I want matlab to round to the nearest value in excel.



No products are associated with this question.

1 Answer

Answer by Andrei Bobrov
on 11 Mar 2013
Edited by Andrei Bobrov
on 11 Mar 2013
 Accepted answer

x = randi([137,149],10,1);
out =  round(x/5)*5;


s = xlsread('yourfile.xlsx'); % data from Excel -> [140;155;166]
x = randi([136,166],10,1); %let this is your data
[~,ii] = min(bsxfun(@(x,y)abs(x-y),s(:).',x(:)),[],2);
out = s(ii);


sorry not exactly what i am looking for.


Join the 15-year community celebration.

Play games and win prizes!

Learn more
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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!