Subtract data from two columns and show result in 3rd column
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Jasmine
2014년 7월 23일
편집: Jasmine
2014년 7월 23일
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
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
2014년 7월 23일
Read in the data using xlsread() and write it with xlswrite
A = xlsread(____);
A(:,3) = A(:,1)-A(:,2);
xlswrite(____)
댓글 수: 7
Joseph Cheng
2014년 7월 23일
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
2014년 7월 23일
댓글 수: 2
Joseph Cheng
2014년 7월 23일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!