Rounding to a specific number

6 ビュー (過去 30 日間)
carl chew
carl chew 2013 年 3 月 11 日
コメント済み: Ícar 2021 年 7 月 18 日
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.

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 3 月 11 日
編集済み: Andrei Bobrov 2013 年 3 月 11 日
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 件のコメント
Kirankumar Bacchewar
Kirankumar Bacchewar 2020 年 11 月 30 日
@Andrei, could you please explain the function you added.
Ícar
Ícar 2021 年 7 月 18 日
@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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by