For Loop does not work over a specified Range

Dear community,
I try to establish a novel subtable from a datasheet that contains multiple columns I can access by searching two strings.
However, in the final step I have a problem: the for loop I try to run from a specified range (first = column 212, last = column 238) will begin at column 1. The variables between column 1 and 237 will then be stored as zeros. Why is that? And how can I work around it?
Thank you
clearvars ~ data
close all
filepath = ['C:\Hannah\VTA Projekt'];
[~,sheets] = xlsfinfo('Effekte_der_DBS_korrigiert.xlsx');
data = cell(1,numel(sheets));
for s = 1:numel(sheets)
data{s} = readtable('Effekte_der_DBS_korrigiert.xlsx','Sheet',(s));
end
%%
subtable = data{1,3};
T = table2cell(subtable);
str = string(T);
Pattern1 = ('MedOFF_StimON')
getdata1 = endsWith(str,Pattern1);
Pattern2 = ('UPDRS_III')
getdata2 = contains(str,Pattern2);
getdata = getdata1 .* getdata2
[row col] = find(getdata);
first = col(:,1)
last = col(:,end)
for k = 1:row
for j = drange(first:last)
new_table{j} = subtable{:, j}
end
end

Answers (1)

When you execute commands like the following, if myNewVariable didn't exist before the elements in myNewVariable prior to element 7 have to be filled with something. Otherwise you wouldn't be assigning into element 7 of myNewVariable, you'd be assigning into element 1.
For numeric data types, that something is 0. For cell arrays it's {[]}.
myNewVariable(7) = 1
myNewCellArray(7) = {1}

1 Comment

Unfortunately it is not working. I will then always get an empty newCellArray.
clearvars ~ data
close all
filepath = ['C:\Hannah\VTA Projekt'];
[~,sheets] = xlsfinfo('Effekte_der_DBS_korrigiert.xlsx');
data = cell(1,numel(sheets));
for s = 1:numel(sheets)
data{s} = readtable('Effekte_der_DBS_korrigiert.xlsx','Sheet',(s));
end
%%
subtable = data{1,3};
T = table2cell(subtable);
str = string(T);
Pattern1 = ('MedOFF_StimON')
getdata1 = endsWith(str,Pattern1);
Pattern2 = ('UPDRS_III')
getdata2 = contains(str,Pattern2);
getdata = getdata1 .* getdata2
[row col] = find(getdata);
first = col(:,1)
last = col(:,end)
myNewVariable(212) = []
myNewCellArray(212) = {[]}
for k = 1:row
for j = drange(first:last)
myNewCellArray{j} = subtable{:, j}
end
end
The new Array will look like this:
Columns 199 through 209
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
Columns 210 through 227
{0×0 double} {[0]} {96×1 cell} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]}
Columns 228 through 238
{[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]} {[0]}
How can I resolve this issue?
Thank you.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 15 May 2020

Commented:

on 18 May 2020

Community Treasure Hunt

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

Start Hunting!