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 Comments

Why not just change
m=dlmread('input.txt',',');
to
xlrange = something appropriate
m = xlsread('input', 1, xlrange );
Yes I have done this but my problem is that the variables need some change because the code stacks when I try to run it
"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...
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.
Yes, what Walter Roberson describes, is exactly my problem and I don't know how to fix it. If I simply replace " m=dlmread" to "m=xlread", I'm getting this error "Error in test2 (line 6) r(m(1,i),m(1,i+1))=1 "

Sign in to comment.

 Accepted Answer

Well, as Walter so astutely observed, you'll have to decipher how, precisely, the other variables than r are built from it--superfically looking, it appears that they are computable given r.
If you're trying to read r instead of the original text file, then you need to change the code to reflect that you've redefined the required input to match--something similar to
r=xlsread('yourfile'); % read the r array instead of text file of _m_
m=size(r,1); % replace the needed size variable simplest way possible
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)
....
with rest of the code from that point on as it presently exists.
You can try the above to see if it will reproduce the results of the alternate form reading the text file...not guaranteed, but a superficial read indicates it should, I think...
You can probably figure out a way to eliminate the while loop above by find -ing the i,k locations from the array but I didn't really dig into the details of the logic to do so...

1 Comment

[OP Answer moved to comment -- dpb]
Thanks a lot dpb, that was the solution to my problem!!

Sign in to comment.

More Answers (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.

Categories

Asked:

on 3 Nov 2018

Edited:

on 7 Nov 2018

Community Treasure Hunt

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

Start Hunting!