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)
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
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.

Sign in to comment.

Answers (2)

Mathieu NOE
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
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)
M = 15×4
0 3 5 8 7 7 0 2 15 17 3 6 1 9 18 9 7 8 4 16 7 8 0 10 20 11 16 7 11 0 7 14 0 7 13 0 20 14 14 5
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
C = 5×1 cell array
{[ 0 3 5 8]} {4×4 double } {2×4 double } {[11 0 7 14]} {7×4 double }
.

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!