Export every third row from excel.

Hi all, I want to read the excel in matlab and get data of every third row from the first row till the end for all sheets. Each sheet is x and y data for 289 frames. I missed the last two rows of data so I also would like to round 289/3 up to the upper integer. Thank you so much.

2 件のコメント

dpb
dpb 2022 年 4 月 30 日
So show us what you've done and where you had a problem...
>> ceil(289/3)
ans =
97
>>
Yang Hu
Yang Hu 2022 年 4 月 30 日
Hello, this is my code so far. I think it can output the data I want but I don't know how to export it to excel. x1, y1 are the new data I want to output.
close all
global mu
SPACE_UNITS = 'µm';
TIME_UNITS = 'min';
%% If loading in individual sheets
sheets = sheetnames('4.4.22.xlsx');
filename = '4.4.22_15mins'; %update filename to describe
f = 289;
for i=1:size(sheets)
xy=readmatrix('4.4.22.xlsx', 'Sheet',sheets(i), 'Range','A1:B289');
B=xy(~isnan(xy)); %takes out a defect
if size(B, 1)==f*2 % f*2
if i==1
x1=xy(:,1);
y1=xy(:,2);
else
x1=[x1; xy(:,1)];
y1=[y1; xy(:,2)];
end
end
end
%%reshape input x and y
x=reshape(x1, f, []);
y=reshape(y1, f, []);
%% manipulate data in 15 mins time gap
x1 = x(1:3:end,:); %get every third x coordinate
y1 = y(1:3:end,:); %get every third y coordinate
f1 = ceil(f/3);%round up to upper integer

サインインしてコメントする。

 採用された回答

Voss
Voss 2022 年 4 月 30 日

0 投票

sheet = 1;
data = zeros(97,0);
while true
try
new_data = readmatrix('4.4.22.xlsx','sheet',sheet);
catch
break
end
data(:,end+[1 2]) = new_data(1:3:end,:);
sheet = sheet+1;
end
size(data)
ans = 1×2
97 66

6 件のコメント

Yang Hu
Yang Hu 2022 年 4 月 30 日
Thank you very much! I uploaded the code I adjusted in the upper comment.
Voss
Voss 2022 年 4 月 30 日
You're welcome! To export x1 and y1:
writematrix([x1 y1],'result.xlsx')
That will give you all 33 columns of x1 followed by all 33 columns of y1. If you want it to be the first column of x1 followed by the first column of y1, then the second column of each, etc., probably you should modify your code to have them in a single variable. Then export using writematrix.
Yang Hu
Yang Hu 2022 年 5 月 1 日
I made it. Thank you for your help!
Yang Hu
Yang Hu 2022 年 5 月 1 日
Would you mind sharing me your learning experience of using matlab?
Voss
Voss 2022 年 5 月 1 日
You're welcome!
Voss
Voss 2022 年 5 月 3 日
@Yang Hu I didn't mean to ignore your question, I only saw it just now.
To answer: I learned MATLAB over several years, first using it for signal processing stuff in college and later at a job with a heavy MATLAB GUI development component (many years before AppDesigner was introduced). And I learn new aspects of it almost every day still, from seeing some of the creative answers here on this forum.
I don't have a specific reference I can point you to and say, this is how I learned MATLAB (although you may benefit from going through parts of the Onramp). I guess different people learn best through different methods, but for me the best way to learn anything is by trial-and-error: Try something and see what happens, if it doesn't work and you don't know why, read the documentation (if any) to find out. Keep trying things until: (1) you get something that works and you know why it works and under what conditions it will fail, or: (2) you realize that what you wanted to do is not feasible and you understand why, in which case the next step is to think of an alternative way to approach the problem. (Note that none of that is specific to learning MATLAB, but it applies just as it applies to any problem-solving activity - it's the scientific method after all.)

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2021b

質問済み:

2022 年 4 月 30 日

コメント済み:

2022 年 5 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by