Subtract data from two columns and show result in 3rd column

I have the following data in an excel sheet
A B C
736 373
734 371
725 364
719 361
711 353
706 347
699 342
689 337
680 327
676 318
So basically I want to run this as A(1) - B(1),A(2) - B(2)..A(n) -B(n) and it should create the outputs at C1, C2...Cn respectively. Can anyone help me on this?

댓글 수: 4

This is an Excel question. See the Excel Help.
I want to write a script that can do this kind of subtraction. Is it possible?
Yes, this is possible. But why wouldn't you just use Excel?
Anyway you need to read the data into MATLAB
data = xlsread('filename')
for i=1:size(data,1)
output(i) = data(i,1)-data(i,2)
end
fulldata = cat(2,data,output)
xlswrite('filename',fulldata)
Or something like that anyway... syntax may be slightly off, as I haven't tested it.
Benjamin
Benjamin 2019년 4월 15일
편집: Benjamin 2019년 4월 15일
thanks for this helpful answer.
But in every loop, if the size of the matrix (output here) changes Matlab gives error of different matrix sizes. Is there anysolution for that.
FYI: my Matlab code generates a matrix of [A] = n*2 and in every loop (for i = 1:10) n changes. I would like to store all A results in another B matrix.

댓글을 달려면 로그인하십시오.

 채택된 답변

Joseph Cheng
Joseph Cheng 2014년 7월 23일
Read in the data using xlsread() and write it with xlswrite
A = xlsread(____);
A(:,3) = A(:,1)-A(:,2);
xlswrite(____)

댓글 수: 7

KRUNAL
KRUNAL 2014년 7월 23일
편집: KRUNAL 2014년 7월 23일
My file name is dstfile and I tried running this code with columns AA and AB as below :
for i=2:11
X = xlsread(dstfile,'AA: AB');
X(:,3) = X(:,1)-X(:,2);
xlswrite (dstfile,'AC');
end
But it shows me error as
Error using xlsread : Data range 'AA: AB' is invalid.
there is an extra space between the : and AB. you should have 'AA:AB'
Hey I got this answer.Just made some minor yet important changes and it worked.My next issue in this I want to put a condition in this like if
B- A > some number ,
it should write "check values again"
else it should write "no change" in column AC instead of the difference value
For this i would, have
C = B- A;
ind = C== some number; %1's where you want to check values again.
C = cell(size(C)); %make a cell array so you can have different length text.
C(ind) = {'check values again'}; %for the indexes (marked with logical 1) put in this text.
C(~ind) = {'no change'}; %for !ind (inverse) put no change.
then write it to column AC.
I didn't quite get you Joseph. 1st of all tell me how should I convert and make
A(:,3) = A(:,1)-A(:,2)
to
C = B- A;
form?
Did you mean this:
for i=2:11
X = xlsread(dstfile);
X(:,3) = X(:,2)-X(:,1);
Y = int2str (X(:,3));
ind = Y >= 350; %1's where you want to check values again.
Y = cell(size (Y)); %make a cell array so you can have different length text.
Y(ind)= {'check values again'}; %for the indexes (marked with logical 1) put in this text.
Y(~ind)= {'no change'}; %for !ind (inverse) put no change.
xlswrite (dstfile,Y,sheet,'AC');
end
If I write this way, I get output as "no change" in AC, AD and AE column
Why is there a for loop? does dstfile change base on the loop? Also if it outputs is in AC, AD, and AE what is the size of the Y array? is it one column or 3?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KRUNAL
KRUNAL 2014년 7월 23일
I wrote the code as follows :
for i=2:11
X = xlsread(dstfile);
X(:,3) = X(:,2)-X(:,1);
Y = num2str (X(:,3));
if (Y > 300)
Y = {'ok'};
xlswrite (dstfile,Y,sheet,'AC');
else
Y = {'Check'};
xlswrite (dstfile,Y,sheet,'AC');
end
end
But it is updating only cell AC1 and it is writing "check" in that cell.Can anyone suggest where I am going wrong?

댓글 수: 2

because in your if statement you're not going through each Y index. additionally the step above you're going Y = num2str so you're converting it to a string and ask the "STRING" if it is greater than 300.
ok..got my mistake. Finally the code is running..thank you

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2014년 7월 23일

편집:

2019년 4월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by