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:
file path string for load

Subject: file path string for load

From: Kuo-Hsien

Date: 13 Jun, 2012 17:43:07

Message: 1 of 4

Dear all,

A quick question: how to create a file path string for loading many files in with in the same folder?

for example:

a = load ('/data1/HD1/path/subpath/data/subdata/aaa.txt')

I like to create a path string to represent /data1/HD1/path/subpath/data/subdata/

path = /data1/HD1/path/subpath/data/subdata/

So, I can use a = load('path/aaa.txt')


Thanks,
Michael

Subject: file path string for load

From: dpb

Date: 13 Jun, 2012 18:00:04

Message: 2 of 4

On 6/13/2012 12:43 PM, Kuo-Hsien wrote:
> Dear all,
>
> A quick question: how to create a file path string for loading many
> files in with in the same folder?
>
> for example:
>
> a = load ('/data1/HD1/path/subpath/data/subdata/aaa.txt')
>
> I like to create a path string to represent
> /data1/HD1/path/subpath/data/subdata/
>
> path = /data1/HD1/path/subpath/data/subdata/
>
> So, I can use a = load('path/aaa.txt')
...

dname='/data1/HD1/path/subpath/data/subdata/';
file='aaa.txt';
fname=fullfile(dname,file);
a=load(fname);

If you're doing a lot, it may be far simpler to use the results of

d=dir(fullfile(dname,'*.txt'));

and iterate through the members of the .name field returned.

for idx=1:length(d)
   a=load(d(idx).name);
   % process...
end

--

Subject: file path string for load

From: Steven_Lord

Date: 14 Jun, 2012 13:47:37

Message: 3 of 4



"dpb" <none@non.net> wrote in message news:jrakfa$ul4$1@speranza.aioe.org...
> On 6/13/2012 12:43 PM, Kuo-Hsien wrote:

*snip*

> If you're doing a lot, it may be far simpler to use the results of
>
> d=dir(fullfile(dname,'*.txt'));
>
> and iterate through the members of the .name field returned.
>
> for idx=1:length(d)
> a=load(d(idx).name);

You used FULLFILE to add the directory name when you used DIR, so you
should/must use it to add the directory name when you LOAD.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: file path string for load

From: dpb

Date: 14 Jun, 2012 17:39:09

Message: 4 of 4

On 6/14/2012 8:47 AM, Steven_Lord wrote:
>
>
> "dpb" <none@non.net> wrote in message
> news:jrakfa$ul4$1@speranza.aioe.org...
>> On 6/13/2012 12:43 PM, Kuo-Hsien wrote:
>
> *snip*
>
>> If you're doing a lot, it may be far simpler to use the results of
>>
>> d=dir(fullfile(dname,'*.txt'));
>>
>> and iterate through the members of the .name field returned.
>>
>> for idx=1:length(d)
>> a=load(d(idx).name);
>
> You used FULLFILE to add the directory name when you used DIR, so you
> should/must use it to add the directory name when you LOAD.

Good catch, Steve--thanks! :)

I would generally change the working directory to avoid the need either
place and so wasn't on my radar in the use of the name field in the
results of dir().

I probably should have mentioned that to OP as well...

--

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