필터 지우기
필터 지우기

For Loop does not work over a specified Range

조회 수: 1 (최근 30일)
Hannah_Mad
Hannah_Mad 2020년 5월 15일
댓글: Hannah_Mad 2020년 5월 18일
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

답변 (1개)

Steven Lord
Steven Lord 2020년 5월 15일
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
Hannah_Mad
Hannah_Mad 2020년 5월 18일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by