How to read data in excel file and make if statement to build another column
1 回表示 (過去 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!