How can I change the input method by using xlsread and convert the code?
이전 댓글 표시
I have this DFS algorithm code down below, and I wanna convert it properlly so it would read an excel file "input.xls" with the command "m=xlsread('input', 1, xlrange )" which contains the following table and then use it as input to run the DFS. Any ideas?
the table is something like this:
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
and the beginning of the code I want to convert:
m=dlmread('input.txt',',');
r=zeros(m(1,1),m(1,1));
i=2;
while (i+1<=length(m))
r(m(1,i),m(1,i+1))=1; % creates a table with 0 and 1 %
i=i+2;
end
R=r;
dfnr=zeros(1,m(1,1));
T=zeros(1,m(1,1));
stack=zeros(1,m(1,1));
to3odentrou=R;
k=1;
j=1;
while (r(k,j)==0) % check the table to find where there are no zeros %
j=j+1;
if (j==m(1,1)+1)
k=k+1;
j=1;
end
end
r(k,j)=0; % last checked element
%
a=1;
stack(1,a)=k;
stack(1,a+1)=j;
if (stack(1,a)==1)
T(1,k)=0;
T(1,j)=1;
else
T(1,k)=(stack(1,a))-1;
T(1,j)=(T(1,k))-1;
end
댓글 수: 5
Walter Roberson
2018년 11월 3일
Why not just change
m=dlmread('input.txt',',');
to
xlrange = something appropriate
m = xlsread('input', 1, xlrange );
John Smith
2018년 11월 3일
dpb
2018년 11월 3일
"the variables need some change because the code stacks"
What in the world does the above mean, precisely?
SHOW us what the problem is, specifically...
Walter Roberson
2018년 11월 3일
It appears that the input expected for this routine is a matrix of data that contains size and link information, and it appears that you would like to instead directly import the results of building the r matrix. There are, however, a few important variables that are built along the way, and someone would need to analyze to figure out if they can be built just from the completed r matrix.
John Smith
2018년 11월 5일
채택된 답변
추가 답변 (1개)
Alternatively, rather than trying to fix code that has no comments explaining what is going on and poor variable names, you could just get rid of it all, and use the built-in dfsearch. Most likely, something like:
g = digraph(xlsread('input', 1, xlrange));
T = dfsearch(g, 1, 'allevents')
with the appropriate start node and other options for dfsearch.
카테고리
도움말 센터 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!