Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
can sting variables run through loops question

Subject: can sting variables run through loops question

From: Sean Douglas

Date: 27 Jul, 2010 15:48:03

Message: 1 of 14

hello and thanks for previous help...
I am trying to run an array of string varialbes through a for loop. I have a program that is working when I pull in 2 excel files into matlab and then run them agains each other , but now i want to set the program up to pull in mulitples files into matlab and then to run the different files against each other. I am trying to do this with a for loop with string variables inside the loop.

format long
for xx= ['TSO' 'VLO']
  for yy=['OXY' 'USO']
 
start1=20070101
upto1=20100601

[num, txt]=xlsread('yy');
tday1=txt(2:end, 1);
tday1=datestr(datenum(tday1, 'mm/dd/yyyy'), 'yyyymmdd');
tday1=str2double(cellstr(tday1));
adjcls1=num(:, end);
  
[num2, txt2]=xlsread('xx);
tday2=txt2(2:end, 1);
tday2=datestr(datenum(tday2, 'mm/dd/yyyy'), 'yyyymmdd');
tday2=str2double(cellstr(tday2));
adjcls2=num2(:, end);

.....
i am having trouble with this and i am getting errors:

??? Error using ==> xlsread at 219
XLSREAD unable to open file A.
File C:\Users\Lin\Documents\MATLAB\A.xls not found.

Error in ==> chancointLooptest at 21
[num, txt]=xlsread(yy);


below is the relevant part of xlread at 219, so you can look for the problem
%==============================================================================
% handle requested Excel workbook filename
try
    file = validpath(file,'.xls');
catch exception
    err = MException('MATLAB:xlsread:FileNotFound','XLSREAD unable to open file %s.\n%s',...
                           file,exception.message);
    throw(err); %%%% THIS IS LINE 219
end
%=====================================================

please help, thanks

Subject: can sting variables run through loops question

From: Jan Simon

Date: 27 Jul, 2010 16:16:26

Message: 2 of 14

Dear Sean,
 
> for xx= ['TSO' 'VLO']
> for yy=['OXY' 'USO']

Strange. Are you sure you want to get these values for xx and yy?!
  
> [num, txt]=xlsread('yy');

This command calls Matlab's toolbox function XLSREAD for the file 'yy'. Do you mean the variable yy?!

> ??? Error using ==> xlsread at 219
> XLSREAD unable to open file A.
> File C:\Users\Lin\Documents\MATLAB\A.xls not found.

It is not understandable from your code, why Matlab tries to open the file "A.xls".
 
> Error in ==> chancointLooptest at 21
> [num, txt]=xlsread(yy);

This line does not appear in the posted code!

> below is the relevant part of xlread at 219, so you can look for the problem

Do you mean "xlsread" instead of "xlread"? It is not needed to post parts of Matlab's toolbox functions.

Deeply confused, Jan

Subject: can sting variables run through loops question

From: Matt Fig

Date: 27 Jul, 2010 16:29:22

Message: 3 of 14

There are several strange things about what you post. First of all, do you understand what you are getting here?

for xx= ['TSO' 'VLO'] ,xx,end


Next, the line which is shown as the line causing the error does not appear in the code you post. Note that you have the line which causes the error identified as:

[num, txt]=xlsread(yy);

but the closest line in your code to this is:

[num, txt]=xlsread('yy');

which is not the same line.

I can see no reason why your code should pass the filename 'A' to xlsread.

What have you left out?

Subject: can sting variables run through loops question

From: Sean Douglas

Date: 27 Jul, 2010 16:40:08

Message: 4 of 14

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i2n0oq$kr2$1@fred.mathworks.com>...
> Dear Sean,
>
> > for xx= ['TSO' 'VLO']
> > for yy=['OXY' 'USO']
>
> Strange. Are you sure you want to get these values for xx and yy?!
>
> > [num, txt]=xlsread('yy');
>
> This command calls Matlab's toolbox function XLSREAD for the file 'yy'. Do you mean the variable yy?!
>
> > ??? Error using ==> xlsread at 219
> > XLSREAD unable to open file A.
> > File C:\Users\Lin\Documents\MATLAB\A.xls not found.
>
> It is not understandable from your code, why Matlab tries to open the file "A.xls".
>
> > Error in ==> chancointLooptest at 21
> > [num, txt]=xlsread(yy);
>
> This line does not appear in the posted code!
>
> > below is the relevant part of xlread at 219, so you can look for the problem
>
> Do you mean "xlsread" instead of "xlread"? It is not needed to post parts of Matlab's toolbox functions.
>
> Deeply confused, Jan

Yes you should be confused, im really sorry to have wasted your time..
your right, I actually did call the variable yy and xx here is what me code actually looks like:

format long g
start1=20070101
upto1=20100601

for xx=['APC' 'BHI']
 for yy=['ATW' 'USO']

[num, txt]=xlsread(yy); %% THIS IS LINE 21
tday1=txt(2:end, 1);
tday1=datestr(datenum(tday1, 'mm/dd/yyyy'), 'yyyymmdd');
tday1=str2double(cellstr(tday1));
adjcls1=num(:, end);
  
[num2, txt2]=xlsread(xx);
tday2=txt2(2:end, 1);
tday2=datestr(datenum(tday2, 'mm/dd/yyyy'), 'yyyymmdd');
tday2=str2double(cellstr(tday2));
adjcls2=num2(:, end);


this is the end of code and here is the error (i did show the correct error before):
??? Error using ==> xlsread at 219
XLSREAD unable to open file A.
File C:\Users\Lin\Documents\MATLAB\A.xls not found.

Error in ==> chancointLooptest at 21
[num, txt]=xlsread(yy);
 
here is my line 21:
[num, txt]=xlsread(yy)

where i ask matlab to open variable yy, which i think the yy variable should indicate file 'ATW' , it seems maybe matlab is only reading the first letter 'A' for some reason.
this code works fine without the for loop, but that is where this problem seems to start. in case this also was not clear , I want to run the code on many files so i want the file names in a for loop.

thank you

Subject: can sting variables run through loops question

From: Matt Fig

Date: 27 Jul, 2010 17:07:06

Message: 5 of 14

"Sean Douglas" <seanjdouglas@hotmail.com> wrote in message
> where i ask matlab to open variable yy, which i think the yy variable should indicate file 'ATW' , it seems maybe matlab is only reading the first letter 'A' for some reason.
> this code works fine without the for loop, but that is where this problem seems to start. in case this also was not clear , I want to run the code on many files so i want the file names in a for loop.
>
> thank you

This is why I wrote what I did in my first response. Look at this:

for xx= {'TSO' 'VLO'} ,xx,end

Subject: can sting variables run through loops question

From: Sean Douglas

Date: 27 Jul, 2010 17:33:10

Message: 6 of 14

"Matt Fig" <spamanon@yahoo.com> wrote in message <i2n3nq$8st$1@fred.mathworks.com>...
> "Sean Douglas" <seanjdouglas@hotmail.com> wrote in message
> > where i ask matlab to open variable yy, which i think the yy variable should indicate file 'ATW' , it seems maybe matlab is only reading the first letter 'A' for some reason.
> > this code works fine without the for loop, but that is where this problem seems to start. in case this also was not clear , I want to run the code on many files so i want the file names in a for loop.
> >
> > thank you
>
> This is why I wrote what I did in my first response. Look at this:
>
> for xx= {'TSO' 'VLO'} ,xx,end

Hey Matt,

ok, I changed the brackets
i maybe dont know what you mean by : ",xx,end" but i think you are just indicating i need to have xx show up later and then end the for loop. Since this is not helping, I have tried different variations of what you wrote.

this is what i now have:
clear;
format long g
start1=20070101
upto1=20100601

for xx={'APC' 'BHI'};
  for yy={'ATW' 'USO'};

[num, txt]=xlsread(yy);
tday1=txt(2:end, 1);
tday1=datestr(datenum(tday1, 'mm/dd/yyyy'), 'yyyymmdd');
tday1=str2double(cellstr(tday1));
adjcls1=num(:, end);
  
[num2, txt2]=xlsread(xx);
tday2=txt2(2:end, 1);
tday2=datestr(datenum(tday2, 'mm/dd/yyyy'), 'yyyymmdd');
tday2=str2double(cellstr(tday2));
adjcls2=num2(:, end);

...still getting errors:
??? Error using ==> xlsread at 122
Filename must be a string.

Error in ==> chancointLooptest at 21
[num, txt]=xlsread(yy);

im still trying to read about putting strings for files into for loops, but im not finding the most relevant articles on this.
thanks Matt

Subject: can sting variables run through loops question

From: us

Date: 27 Jul, 2010 17:38:05

Message: 7 of 14

On Jul 27, 7:33 pm, "Sean Douglas" <seanjdoug...@hotmail.com> wrote:
> "Matt Fig" <spama...@yahoo.com> wrote in message <i2n3nq$8s...@fred.mathworks.com>...
> > "Sean Douglas" <seanjdoug...@hotmail.com> wrote in message
> > > where i ask matlab to open variable yy, which i think the yy variable should indicate file 'ATW'  , it seems maybe matlab is only reading the first letter  'A' for some reason.    
> > > this code works fine without the for loop, but that is where this problem seems to start. in case this also was not clear , I want to run the code on many files so i want the file names in a for loop.
>
> > > thank you
>
> > This is why I wrote what I did in my first response.  Look at this:
>
> > for xx= {'TSO' 'VLO'} ,xx,end
>
> Hey Matt,
>
> ok, I changed the brackets

>   for yy={'ATW' 'USO'};
> [num, txt]=xlsread(yy);

> ...still getting errors:
> ??? Error using ==> xlsread at 122
> Filename must be a string.

learn to LOOK at things(!)...

- what is yy...
- then: THINK
- then: THINK, again
- what would yy{1} be...
- then: POST (maybe)

us

Subject: can sting variables run through loops question

From: Matt Fig

Date: 27 Jul, 2010 17:40:32

Message: 8 of 14

"Sean Douglas" <seanjdouglas@hotmail.com> wrote in message
> ok, I changed the brackets
> i maybe dont know what you mean by : ",xx,end" but i think you are just indicating i need to have xx show up later and then end the for loop. Since this is not helping, I have tried different variations of what you wrote.


I meant for you to copy that line into the command window and look at what you get versus what you originally had. You are supposed to realize that you need to use a cell array.

Try these (copy and paste):

% Alternative 1
D = {'ABC', 'DEF'}; for xx=1:2 ,D{xx},end

% Alternative 2
for xx= {'ABC', 'DEF'} ,xx{1},end

Now you should get strings, not cells, in the loop.

Subject: can sting variables run through loops question

From: Andy

Date: 27 Jul, 2010 17:45:24

Message: 9 of 14

Try this:

for yy = {'ATW' 'USO'}
    yy
    class(yy) % important!
end

The error message is, of course, correct. The variable yy is not a string. You can instead try:

for yy = {'ATW' 'USO'}
    [num, txt] = xlsread(yy{1});
end

You should try to do this simpler debugging on your own without resorting to posting to the newsgroup. You'll learn the language much faster if you see what it's doing during basic operations. Type

dbstop if error

at the command line before running your script. Then, if it errors, it will stop executing and allow you to examine the values of the variables in the workspace to see what the problem is.

Subject: can sting variables run through loops question

From: Jan Simon

Date: 27 Jul, 2010 21:58:05

Message: 10 of 14

Dear Matt Fig,

> for xx= {'TSO' 'VLO'} ,xx,end

Is this a documented feature? It worked in Matlab 6.5 already, but I cannot find a hint in the help or doc.

Deeply impressed to learn something new about such a basic function, Jan

Subject: can sting variables run through loops question

From: us

Date: 27 Jul, 2010 22:13:04

Message: 11 of 14

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i2nkpd$r4b$1@fred.mathworks.com>...
> Dear Matt Fig,
>
> > for xx= {'TSO' 'VLO'} ,xx,end
>
> Is this a documented feature? It worked in Matlab 6.5 already, but I cannot find a hint in the help or doc.
>
> Deeply impressed to learn something new about such a basic function, Jan

huh(!?)...
you MUST be kidding, jan...

:-)
urs

Subject: can sting variables run through loops question

From: Matt Fig

Date: 27 Jul, 2010 22:14:20

Message: 12 of 14

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i2nkpd$r4b$1@fred.mathworks.com>...
> Dear Matt Fig,
>
> > for xx= {'TSO' 'VLO'} ,xx,end
>
> Is this a documented feature? It worked in Matlab 6.5 already, but I cannot find a hint in the help or doc.
>
> Deeply impressed to learn something new about such a basic function, Jan



Yes, in the doc for the FOR function:


"The line
for V = A, ..., end
has the same effect as
for k = 1:n, V = A(:,k); ..., end
except k is also set here."

Subject: can sting variables run through loops question

From: Jan Simon

Date: 27 Jul, 2010 22:36:04

Message: 13 of 14

Dear Urs,

> > > for xx= {'TSO' 'VLO'} ,xx,end

> > Deeply impressed to learn something new about such a basic function, Jan
> you MUST be kidding, jan...

No, not kidding, Urs. FOR is *really* basic.

In fact, the help of FOR claims:
"FOR variable = expr, ... The columns of the expression are stored one at a time ...". I've done this with row vectors and matrices, but never with cell or struct arrays.

Wow. I think I should look into the "Getting started" again to find the other gems. Perhaps I'll find even a function, which can evaluate a string as if it would be typed in the command line? It would be so powerful. Or some lessons about English grammar?

Good night, Jan

Subject: can sting variables run through loops question

From: Steven_Lord

Date: 28 Jul, 2010 15:19:11

Message: 14 of 14



"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
news:i2nn0k$kni$1@fred.mathworks.com...
> Dear Urs,
>
>> > > for xx= {'TSO' 'VLO'} ,xx,end
>
>> > Deeply impressed to learn something new about such a basic function,
>> > Jan
>> you MUST be kidding, jan...
>
> No, not kidding, Urs. FOR is *really* basic.

But even basic functions can have surprising (hopefully in a good way)
behavior, even for experienced users.

> In fact, the help of FOR claims:
> "FOR variable = expr, ... The columns of the expression are stored one at
> a time ...". I've done this with row vectors and matrices, but never with
> cell or struct arrays.

Yes, that's why that code worked. The FOR loop is iterating through the
columns of the 1-by-2 cell array; first xx takes on the value of the first
column of the cell array, aka {'TSO'} then it takes on the second column
{'VLO'}. Neat, huh?

> Wow. I think I should look into the "Getting started" again to find the
> other gems. Perhaps I'll find even a function, which can evaluate a string
> as if it would be typed in the command line? It would be so powerful.

To quote Uncle Ben: "With great power comes great responsibility" ... in
this case the responsibility to keep people from using EVAL when unnecessary
or dangerous :)

> Or some lessons about English grammar?
>
> Good night, Jan

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us