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일

0 개 추천

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

carl chew
carl chew 2013년 3월 11일
sorry not exactly what i am looking for.
Andrei Bobrov
Andrei Bobrov 2013년 3월 11일
added.
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개)

카테고리

질문:

2013년 3월 11일

댓글:

2021년 7월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by