How to read data in excel file and make if statement to build another column
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello, I am trying to do some post processing in MATLAB
Let's say there is a excel file contains information like below
time x y z
1 x1 y1 z1
2 x2 y2 z2
3 x3 y3 z3
4 x4 y4 z4
..........
What I am trying to do is read the information in specific location, and make a if statement to build another column. For example, I want to find y that has value between 1 and 2, and from that point make another column starting from that row containing y value satisfies the condition and save it. What shall I do?
time x y z a
1 x1 y1 z1 a1
2 x2 y2 z2 a2
3 x3 y3 z3 a3
4 x4 y4 z4 a4 ..........
댓글 수: 0
답변 (1개)
Guillaume
2016년 11월 24일
What you should do is not use an if statement and rather use the proper functions. It's not exactly clear what the final aim. This should get you started
%load excel file using readtable (a lot more powerful than xlsread)
data = readtable('c:\\somewhere\somefile.xlsx'); %should result in a table with columns time, x, y, z
%locate rows where y is between 1 and 2
isinrange = data.y >= 1 & data.y <= 2; %no if needed! isinrange is a logical array
%not sure what you want to do from here, let's create a new column that is x+y when y is in range:
data.a = zeros(height(data), 1); %create new column full of 0s
data.a(isinrange) = data.x(isinrange) + data.y(isinrange)
%or let's fill column a with value from the 1st row where y is in range:
data.a = zeros(height(data), 1); %create new column (or reset it)
startrow = find(isinrange, 1); %find index of first row that is in range
data.a(startrow:end) = 1 : height(data)-startrow+1
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!