Borrar filtros
Borrar filtros

How to loop a process and end when cell array is empty?

11 visualizaciones (últimos 30 días)
Calabrese
Calabrese el 15 de Jul. de 2017
Respondida: James Tursa el 15 de Jul. de 2017
I am running a process where I read an excel file with many rows. The rows consist of package data that vary in how many rows each package consists of. I separate the 1st package out of the cell array, process it, and write it to an excel file. When I pull the 1st package out, I reassign the variable of the original cell array to the new cell array that no longer has the 1st package in it. I would like to repeat the process until the original cell array is empty.
% Time
clear all
clc
[file, folder] = uigetfile('*.xlsx');
[num,txt,raw] = xlsread(file);
l_raw = length(raw);
l_raw = l_raw - 1;
raw = [raw(5:l_raw,1), raw(5:l_raw,6), raw(5:l_raw,7), raw(5:l_raw,8), raw(5:l_raw,10)];
%create new file
headers = [cellstr('Package'), cellstr('MR Time'), cellstr('Al Time')];
xlswrite('Span time.xlsx', headers, 1)
%%loop this
%Seperate based on data in first column
what = raw(1,1);
idx = cellfun(@(x) strcmp(x, what), raw(:,1));
% Extracted data
SR = raw(idx,:);
% Remove Close SR row
l_SR = length(SR);
l_SR = l_SR - 1;
SR = SR(1:l_SR,:);
% Remaining package (should be raw)
raw = raw(~idx,:);
%Split extracted data based on strings
S = {'Update', 'Quality', 'Rep'};
m = size(SR,1);
y = true(m,1);
for k=1:m
x = cellfun(@ischar,SR(k,:));
y(k) = any(ismember(SR(k,x),S));
end
Al = SR(y,:);
FW = SR(~y,:);
%sum span times
time_Al = sum(cell2mat(Al(:,5)));
time_MR = sum(cell2mat(FW(:,5)));
%group extracted data and span times
package = [SR(1,1), time_MR, time_Al];
%read excel file and find next available row
[num2,txt2,raw2] = xlsread('Span time.xlsx');
[nRows,nCol] = size(raw2);
nRows2 = nRows +1;
s = num2str(nRows2);
%write to next available row
xlswrite('Span time.xlsx', package,1,s);
%Repeat until raw is empty
It would probably be more efficient to write the 'package' to a matrix in MATLAB and continue to append it and then write it all at once to the excel file but I don't know how to do that.

Respuesta aceptada

James Tursa
James Tursa el 15 de Jul. de 2017
Something like this?
while( true )
% Your code goes here
if( isempty(raw) )
break;
end
end

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by