gisuser_14213
gisuser_14213

Reputation: 75

Issues with fopen understanding the filepath I am giving it

Basically, I build a list of files using ls, then want to loop through that list, read in a file, and do some stuff. But when I try to read the file in it fails.

Here is an example

r=ls(['Event_2006_334_21_20_11' '/*.r'])

Event_2006_334_21_20_11/IU.OTAV_1.0.i.r

which is a 1x80 char

fopen(r(1,:))

-1

but

fopen('Event_2006_334_21_20_11/IU.OTAV_1.0.i.r')

12 (or whatever its on)

works. I've tried string(r) and char(r) and sprintf('%s',r). If I just build the string like r = ['Event_2006_334_21_20_11' '/IU.OTAV_1.0.i.r'] it works. So it seems something about combining the different variable types that messes it up but I can't seem to find a workaround. Probably something obvious I'm missing.

Any suggestions?

Upvotes: 0

Views: 34

Answers (1)

Daniel
Daniel

Reputation: 36710

ls returns a matrix of characters, which means each row contains the same number of characters. To indicate the problem, try:

['-' r(1,:) '-']

You will probably notice some whitespaces in front of the -. Unless you want to print the output to the command line, ls is not really useful. As mentioned by Alex, use dir instead.

A further tip regarding your last comment, concatenate file path using fullfile. It makes sure you get one file separator whenever concatenating:

>> fullfile('myfolder','mysubfolder','myfile.m')
ans = myfolder/mysubfolder/myfile.m
>> fullfile('myfolder/','mysubfolder','myfile.m')
ans = myfolder/mysubfolder/myfile.m
>> fullfile('myfolder/','/mysubfolder','myfile.m')
ans = myfolder/mysubfolder/myfile.m

Upvotes: 1

Related Questions