Azzera filtri
Azzera filtri

For Loop does not work over a specified Range

1 visualizzazione (ultimi 30 giorni)
Hannah_Mad
Hannah_Mad il 15 Mag 2020
Commentato: Hannah_Mad il 18 Mag 2020
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

Risposte (1)

Steven Lord
Steven Lord il 15 Mag 2020
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 Commento
Hannah_Mad
Hannah_Mad il 18 Mag 2020
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.

Accedi per commentare.

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by