Rounding to a specific number

6 Ansichten (letzte 30 Tage)
carl chew
carl chew am 11 Mär. 2013
Kommentiert: Ícar am 18 Jul. 2021
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.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 11 Mär. 2013
Bearbeitet: Andrei Bobrov am 11 Mär. 2013
x = randi([137,149],10,1);
out = round(x/5)*5;
ADD
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);
  4 Kommentare
Kirankumar Bacchewar
Kirankumar Bacchewar am 30 Nov. 2020
@Andrei, could you please explain the function you added.
Ícar
Ícar am 18 Jul. 2021
@Kirankumar Bacchewar that function computes the error between each position in s and x (the data points), and looks for the minimum error, meaning the two values that are closest together.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by