Hi , all there , i have an excel file that i want to split it by special element value in the file to some files . how can i make it possoble with matlab
1 view (last 30 days)
Show older comments
The initial excel file is composed of 5678 rows and 4 columns. what i want is to split it to some subfiles according to particular principle . for example '0' in a row
how should i do it in matlab ?
THX
Frank
1 Comment
Jan
on 3 Mar 2023
What have you tried so far? Can you import the file already? Then the input is a matrix already and you can omit the "initial excel file" part.
Answers (2)
Mathieu NOE
on 6 Mar 2023
hello
try this
I created first a dummy array with some zeros at lines 25 / 75 / 85
so the for loop will split the data with rows going from 1 to 24 then 25 to 74 , 75 to 84 , 85 to end of array (100th row)
each individual array is stored in a separate excel file
hope this helps
%% 1/ create some dummy data and store in a excel file
A = rand(100,5);
A(:,1) = (1:100)';
% create some specific (0) values
target_val = 0;
A(25,3) = target_val;
A(75,2) = target_val;
A(85,5) = target_val;
writematrix(A,"dummy.xlsx");
%% 2/ main code
B = readmatrix("dummy.xlsx");
[m,n] = size(B);
tol = 1e-6; % define tolerance
[r,c] = find(abs(B-target_val)<tol);
r = sort(r); % sort r in ascending order
rr = [1;r;m+1];
for ci = 1:numel(rr)-1
start_row = rr(ci)
stop_row = rr(ci+1)-1
data = B(start_row:stop_row,:); % extract rows
filename_out = ['out' num2str(ci) '.xlsx'];
writematrix(data,filename_out);
end
Star Strider
on 6 Mar 2023
To begin a new sub-array whenever a zero appears in any row, this works —
M = randi([0 20], 15, 4)
ZeroInRow = any(M==0,2); % Returns 'true' For Any Row With A '0'
Idx = cumsum(ZeroInRow); % Define Grouped Rows By Cumulative Sum
[Lb,Ub] = bounds(Idx); % Number Of Instances Of Rows With '0'
StartIdx = Lb;
for k = Lb:Ub
cidx = k-StartIdx+1;
C{cidx,:} = M(Idx==k,:); % Create Cell Array Of Submatrices Matching Condition
end
C
.
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!