Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: loading files using eval ' problem

Subject: loading files using eval ' problem

From: Pep

Date: 17 Jun, 2008 09:11:09

Message: 1 of 7

Hi there!

I have the following problem:

I can upload some files which have the following format:

initp1 = load('initial_parameters1.txt');
initp2 = load('initial_parameters2.txt');
initp3 = load('initial_parameters3.txt');
initp4 = load('initial_parameters4.txt');

I am trying to use eval

for i = 1:4

eval(['initp',
num2str(i),'=load(initial_parameters',num2str(i),'.txt);'])

end

But since I need to insert the ' character after the load and after
the txt' I can not use it.

I am in the directory where the files are stored /global/proddatadg/
sdpe/production/MBS/devel/fmsu_pdfs/pdf_results/init_parameters
(In fact I can do initp4 = load('initial_parameters4.txt'); with no
problems)

and I have tried to use
addpath('/global/proddatadg/sdpe/production/MBS/devel/fmsu_pdfs/
pdf_results/init_parameters");

Does anyone know how to solve the ' problem?

Thank you very much in advance.

Kind regards,

Pep

Subject: loading files using eval ' problem

From: Piter_

Date: 17 Jun, 2008 09:19:21

Message: 2 of 7

On Jun 17, 11:11 am, Pep <peppuigv...@gmail.com> wrote:
> Hi there!
>
> I have the following problem:
>
> I can upload some files which have the following format:
>
> initp1 = load('initial_parameters1.txt');
> initp2 = load('initial_parameters2.txt');
> initp3 = load('initial_parameters3.txt');
> initp4 = load('initial_parameters4.txt');
>
> I am trying to use eval
>
> for i = 1:4
>
> eval(['initp',
> num2str(i),'=load(initial_parameters',num2str(i),'.txt);'])
>
> end
>
> But since I need to insert the ' character after the load and after
> the txt' I can not use it.
>
> I am in the directory where the files are stored /global/proddatadg/
> sdpe/production/MBS/devel/fmsu_pdfs/pdf_results/init_parameters
> (In fact I can do initp4 = load('initial_parameters4.txt'); with no
> problems)
>
> and I have tried to use
> addpath('/global/proddatadg/sdpe/production/MBS/devel/fmsu_pdfs/
> pdf_results/init_parameters");
>
> Does anyone know how to solve the ' problem?
>
> Thank you very much in advance.
>
> Kind regards,
>
> Pep

Hi
You don't need commas.
eval(['initp'num2str(i)'=load(initial_parameters' num2str(i)
'.txt);'])
I think this will work.
Best.
Petro.

Subject: loading files using eval ' problem

From: French Caro

Date: 17 Jun, 2008 09:23:01

Message: 3 of 7

Pep <peppuigvert@gmail.com> wrote in message
<9d68855a-99ab-4c64-bd0c-2d9740381f7f@x41g2000hsb.googlegroups.com>...
> Hi there!
>
> I have the following problem:
>
> I can upload some files which have the following format:
>
> initp1 = load('initial_parameters1.txt');
> initp2 = load('initial_parameters2.txt');
> initp3 = load('initial_parameters3.txt');
> initp4 = load('initial_parameters4.txt');
>
> I am trying to use eval
>
> for i = 1:4
>
> eval(['initp',
> num2str(i),'=load(initial_parameters',num2str(i),'.txt);'])
>
> end
>
> But since I need to insert the ' character after the load
and after
> the txt' I can not use it.
>
> I am in the directory where the files are stored
/global/proddatadg/
>
sdpe/production/MBS/devel/fmsu_pdfs/pdf_results/init_parameters
> (In fact I can do initp4 =
load('initial_parameters4.txt'); with no
> problems)
>
> and I have tried to use
>
addpath('/global/proddatadg/sdpe/production/MBS/devel/fmsu_pdfs/
> pdf_results/init_parameters");
>
> Does anyone know how to solve the ' problem?
>
> Thank you very much in advance.
>
> Kind regards,
>
> Pep
Why use eval ? It is a bad way.
Use cell array instead :
for i=1:4
   initp{i} = load(sprintf('initial_parameters%d.txt',i));
end

BTW to put a ' in a string you just have to double it e.g. :
str='string with '' inside and at the end'''

Caroline

Subject: loading files using eval ' problem

From: Pep

Date: 17 Jun, 2008 09:24:10

Message: 4 of 7

On 17 Juny, 11:19, Piter_ <x.pi...@gmail.com> wrote:
> On Jun 17, 11:11 am, Pep <peppuigv...@gmail.com> wrote:
>
>
>
>
>
> > Hi there!
>
> > I have the following problem:
>
> > I can upload some files which have the following format:
>
> > initp1 = load('initial_parameters1.txt');
> > initp2 = load('initial_parameters2.txt');
> > initp3 = load('initial_parameters3.txt');
> > initp4 = load('initial_parameters4.txt');
>
> > I am trying to use eval
>
> > for i = 1:4
>
> > eval(['initp',
> > num2str(i),'=load(initial_parameters',num2str(i),'.txt);'])
>
> > end
>
> > But since I need to insert the ' character after the load and after
> > the txt' I can not use it.
>
> > I am in the directory where the files are stored /global/proddatadg/
> > sdpe/production/MBS/devel/fmsu_pdfs/pdf_results/init_parameters
> > (In fact I can do initp4 = load('initial_parameters4.txt'); with no
> > problems)
>
> > and I have tried to use
> > addpath('/global/proddatadg/sdpe/production/MBS/devel/fmsu_pdfs/
> > pdf_results/init_parameters");
>
> > Does anyone know how to solve the ' problem?
>
> > Thank you very much in advance.
>
> > Kind regards,
>
> > Pep
>
> Hi
> You don't need commas.
> eval(['initp'num2str(i)'=load(initial_parameters' num2str(i)
> '.txt);'])
> I think this will work.
> Best.
> Petro.- Amaga el text entre cometes -
>
> - Mostra el text citat -

>> eval(['initp'num2str(i)'=load(initial_parameters' num2str(i) '.txt);'])
??? eval(['initp'num2str(i)'=load(initial_parameters' num2str(i)
'.txt);'])
                 |
Error: Unexpected MATLAB expression.
Thanks Petro unfortunately this is not still working. Any other hint
it will much appreciated.
Pep

Subject: loading files using eval ' problem

From: French Caro

Date: 17 Jun, 2008 09:47:01

Message: 5 of 7

Pep <peppuigvert@gmail.com> wrote in message
<snip>
>
> >> eval(['initp'num2str(i)'=load(initial_parameters'
num2str(i) '.txt);'])
> ??? eval(['initp'num2str(i)'=load(initial_parameters'
num2str(i)
> '.txt);'])
> |
> Error: Unexpected MATLAB expression.
> Thanks Petro unfortunately this is not still working. Any
other hint
> it will much appreciated.
> Pep

Have you seen my message (number 3) ?
I have posted at the mostly same time than Piter_ so maybe
you didn't see it.

Caroline

Subject: loading files using eval ' problem

From: Jos

Date: 17 Jun, 2008 09:56:02

Message: 6 of 7

Pep <peppuigvert@gmail.com> wrote in message
<9d68855a-99ab-4c64-bd0c-2d9740381f7f@x41g2000hsb.googlegroups.com>...
> still using eval, after

Just to highlight Caroline's remark: Do not use eval!

As you have found out already it makes debugging very hard.
For your problem, use the function form of LOAD, as in

i = 3 ;
S = load(['name' num2str(i) '.mat']) ;

hth
Jos

Subject: loading files using eval ' problem

From: Pep

Date: 17 Jun, 2008 10:24:32

Message: 7 of 7

On 17 Juny, 11:56, "Jos " <DEL...@jasenDEL.nl> wrote:
> Pep <peppuigv...@gmail.com> wrote in message
>
> <9d68855a-99ab-4c64-bd0c-2d9740381...@x41g2000hsb.googlegroups.com>...
>
> > still using eval, after
>
> Just to highlight Caroline's remark: Do not use eval!
>
> As you have found out already it makes debugging very hard.
> For your problem, use the function form of LOAD, as in
>
> i = 3 ;
> S = load(['name' num2str(i) '.mat']) ;
>
> hth
> Jos

Thanks a lot!
Your contributions really enhanced my program.
Kind regards,
Pep

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
avoid eval Jos 17 Jun, 2008 06:00:29
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics