In the given excel I have 4 columns I want to split the data with respect to column 1 name as minutes and save the data of the other corresponding columns name with D1, D2, D3.
the condition should be that the data start from zero in column 1 and select the data upto next zero occure. save this data in one cell and again start saving data start from the next zero upto another zero occure.

댓글 수: 7

Rik
Rik 2021년 10월 27일
A naive approach would be to do this with a loop row by row. When you have working code with a loop it will be likely be easy to speed this up (e.g. with something like find(data>0,1,'first')).
What did you try?
Adil Sardar
Adil Sardar 2021년 10월 27일
편집: Rik 2021년 10월 27일
I tried a simple loop but I am unable to solve it. I used the loop as given but they select all the data except 0.
for i = length(minute)
if minute(i) ~= 0
Save_data = minute(i)
end
end
Just like this but this is not correct. I don't what type of condition I use.
Rik
Rik 2021년 10월 27일
Write it out: what should happen when? What should be done for each row?
If the value of minute is 0, start a new cell and make that the current cell. If not, stay in the current cell.
Then add the value of your data to the current cell.
How would you write this in code? Also, please use the layout options of the editor.
Adil Sardar
Adil Sardar 2021년 10월 27일
I want to save the data of minute column start from first zero upto another zero and save in a cell.
Adil Sardar
Adil Sardar 2021년 10월 27일
Adil Sardar
Adil Sardar 2021년 10월 27일
Just like the above image I want to save the data like this
Rik
Rik 2021년 10월 27일
I understand what you want, now I want to see if you can translate the text I wrote to code. Think of what should happen step by step. Can you understand why the code you posted didn't work?

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

 채택된 답변

Mathieu NOE
Mathieu NOE 2021년 10월 27일

0 개 추천

hello
could not load your excel file , so I created a dummy one (attached FYI)
the table is splitted into cells according to first zero in minute column
you can also convert tables into other structure if you prefer
hope it helps
clc
clearvars
T = readtable('test1.xlsx');
[m,n] = size(T);
minuts = T.Min;
%% define index corresponding to first zeros (repeating)
ind = find(minuts<eps);
dd = [0; diff(ind)];
p = find(diff(ind)>1);
ind_first_zeros = [ind(1); ind(p+1)];
nb_cells = numel(ind_first_zeros);
%% export table extracts in individual cell
for ci =1:nb_cells
ind_start = ind_first_zeros(ci);
if ci < nb_cells
ind_stop = ind_first_zeros(ci+1)-1;
else % last block of data finish at end of file
ind_stop = m;
end
data_cell{ci} = T(ind_start:ind_stop,:);
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

질문:

2021년 10월 27일

댓글:

2021년 10월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by