Matlab is giving me:
"Subscript indices must either be real positive integers or logicals.
Error in testt (line 40)
if idx(x) < idx1(j) && idx(x+1) > idx1(j)"
I already tried to use the function "round" and i dont had success. My program analyze a .txt file (i have upload the file "items.txt") and select specific words like, "bonus_damage", "ItemCost" and "bonus_amor". Than it organize all this data using the position of each data ("idx", "idx1" and "idx2"). The variables "iwant", "iwant1" and "iwant2" gives me the numbers that i want to write into a excel spreadsheet. The if statements like if "idx(x) < idx1(j) && idx(x+1) > idx1(j)" gives the exact location of where i need to write each data.
Thanks
function x=testt()
fid = fopen('items.txt') ;
S = textscan(fid,'%s','delimiter','\n') ;
S = S{1} ;
idx = strfind(S, '"ItemCost"');
idx = find(not(cellfun('isempty',idx)));
ACD = S(idx) ;
b=regexp(ACD,'\d+(\.)?(\d+)?','match') ;
iwant=str2double([b{:}]);
y=size(iwant);
tit{1}='ItemCost';
my_cell = sprintf( 'B%s',num2str(1) );
xlswrite('testitens3.xlsx',tit,'Sheet1',my_cell);
idx1 = strfind(S, '"bonus_damage"');
idx1 = find(not(cellfun('isempty',idx1)));
ACD1 = S(idx1) ;
b1=regexp(ACD1,'\d+(\.)?(\d+)?','match') ;
iwant1=str2double([b1{:}]);
for k=1:y(2)
my_cell = sprintf( 'B%s',num2str(k+1) );
xlswrite('testitens3.xlsx',iwant(k),'Sheet1',my_cell);
end
tit{1}='bonus_damage';
my_cell = sprintf( 'C%s',num2str(1) );
xlswrite('testitens3.xlsx',tit,'Sheet1',my_cell);
j=1;
x=0;
while j<1000
if idx(x) < idx1(j) && idx(x+1) > idx1(j)
my_cell = sprintf( 'C%s',num2str(x+1) );
xlswrite('testitens3.xlsx',iwant1(j),'Sheet1',my_cell);
j=j+1;
end
x=x+1;
if x==y(2)
j=1001;
end
end
idx2 = strfind(S, '"bonus_armor"');
idx2 = find(not(cellfun('isempty',idx2)));
ACD2 = S(idx2) ;
b2=regexp(ACD2,'\d+(\.)?(\d+)?','match') ;
iwant2=str2double([b2{:}]);
idx2=round(idx2);
iwant2=round(iwant2);
tit{1}='bonus_armor';
my_cell = sprintf( 'D%s',num2str(1) );
xlswrite('testitens3.xlsx',tit,'Sheet1',my_cell);
j=1;
x=0;
while j<1000
if idx(x) < idx2(j) && idx(x+1) > idx2(j)
my_cell = sprintf( 'D%s',num2str(x+1) );
xlswrite('testitens3.xlsx',iwant2(j),'Sheet1',my_cell);
j=j+1;
end
x=x+1;
if x==y(2)
j=1001;
end
end
end

 Akzeptierte Antwort

Stephen23
Stephen23 am 14 Dez. 2016
Bearbeitet: Stephen23 am 14 Dez. 2016

0 Stimmen

MATLAB indexing starts at one, not zero, but you have written this (watch the x):
x=0;
while j<1000
if idx(x) < ...

1 Kommentar

Thanks a lot. Was a very simple mistake haha.... There was another error to. Here is the final code:
function x=testt()
fid = fopen('items.txt') ;
S = textscan(fid,'%s','delimiter','\n') ;
S = S{1} ;
idx = strfind(S, '"ItemCost"');
idx = find(not(cellfun('isempty',idx)));
ACD = S(idx) ;
b=regexp(ACD,'\d+(\.)?(\d+)?','match') ;
iwant=str2double([b{:}]);
y=size(iwant);
tit{1}='ItemCost';
my_cell = sprintf( 'B%s',num2str(1) );
xlswrite('testitens3.xlsx',tit,'Sheet1',my_cell);
for k=1:y(2)
my_cell = sprintf( 'B%s',num2str(k+1) );
xlswrite('testitens3.xlsx',iwant(k),'Sheet1',my_cell);
end
idx1 = strfind(S, '"bonus_damage"');
idx1 = find(not(cellfun('isempty',idx1)));
ACD1 = S(idx1) ;
b1=regexp(ACD1,'\d+(\.)?(\d+)?','match') ;
iwant1=str2double([b1{:}]);
t=size(iwant1);
tit{1}='bonus_damage';
my_cell = sprintf( 'C%s',num2str(1) );
xlswrite('testitens3.xlsx',tit,'Sheet1',my_cell);
j=1;
x=1;
while j<1000
if idx(x) < idx1(j) && idx(x+1) > idx1(j)
my_cell = sprintf( 'C%s',num2str(x+1) );
xlswrite('testitens3.xlsx',iwant1(j),'Sheet1',my_cell);
j=j+1;
end
if x==y(2)
j=1001;
end
if j==t(2)
j=1001;
end
x=x+1;
end
idx2 = strfind(S, '"bonus_armor"');
idx2 = find(not(cellfun('isempty',idx2)));
ACD2 = S(idx2) ;
b2=regexp(ACD2,'\d+(\.)?(\d+)?','match') ;
iwant2=str2double([b2{:}]);
t=size(iwant2);
tit{1}='bonus_armor';
my_cell = sprintf( 'D%s',num2str(1) );
xlswrite('testitens3.xlsx',tit,'Sheet1',my_cell);
j=1;
x=1;
while j<1000
if idx(x) < idx2(j) && idx(x+1) > idx2(j)
my_cell = sprintf( 'D%s',num2str(x+1) );
xlswrite('testitens3.xlsx',iwant2(j),'Sheet1',my_cell);
j=j+1;
end
if x==y(2)
j=1001;
end
if j==t(2)
j=1001;
end
x=x+1;
end
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by