Accessing from parent folders!

Hi, I am having troubles reading files from a parent folder. Basically I have set the DIR to the parent folder known as
DIR = 'C:\\BLah\\blah\\blah\\Information Retrieval\\CWKFOLDS';
I want to read the path but the textfile im reading from has it in this format ...
../fold2/d_affinia_1.oneline
how do i read from that using textread. I do not want to learn fopen as I have a deadline to meet and getting my head around that seems too complicated at this stage.
Kind Regards, Tanil

Réponses (2)

Image Analyst
Image Analyst le 13 Déc 2012

0 votes

You will learn fopen() in about 20 seconds - quicker than it took you to write your question and you'll be done far far before your deadline. Then call fgets() or fgetl(). Then call fclose(). Exceedingly simple. Change your double backslashes to single backslashes or single forward slashes. Even Windows understands forward slashes believe it or not. You might use fileparts() and fullfile() to construct a filename.

5 commentaires

Tanil
Tanil le 13 Déc 2012
Thanks, it does seem easier when you know that you need fget or fgetl to actually obtain something. It should really mention that in the documentation online. so in this case assume i want to open
d_affinia_1.oneline
and my DIR is the path all the way to cwkfolds
i say:
textdocument = d_affinia_1.oneline;
fid = fopen(textdocument);
tline = fgets(fid);
fclose(fid);
Tanil
Tanil le 13 Déc 2012
Error using fgets
Invalid file identifier. Use fopen to generate a
valid file identifier.
Error in RUNKNN (line 31)
tline = fgets(fid);
If you just want the entire file as a string, then use
tline = fileread(textdocument);
without any fopen or fclose.
But remember,
textdocument = 'd_affinia_1.oneline'
because you do not have a structure named "d_affinia_1" with a field named "oneline"
Tanil
Tanil le 13 Déc 2012
Modifié(e) : Tanil le 13 Déc 2012
sorry i should explain. the extension .oneline is just to say that the textfile has already been formated so that each word appears on a seperate line. I want each word to be held in an array element for example testTerms.
textdocument = '../fold2/d_affinia_1.oneline';
testTerms = regexp( fileread(textdocument), '[\r\n]', 'split');
testTerms will now be a cell array of strings, one entry per line.

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 13 Déc 2012

0 votes

olddir = cd(DIR);
TheData = textread('../fold2/d_affinia_1.oneline');
cd(olddir);

2 commentaires

Tanil
Tanil le 13 Déc 2012
hmm this seems to work but for example theData.mat gets stored in that directory. I dont think this is a problem but I hope it doesn't become one eventually.
Walter Roberson
Walter Roberson le 13 Déc 2012
I have no idea why theData.mat would get stored in that directory, unless you have a save() somewhere.

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Question posée :

le 13 Déc 2012

Community Treasure Hunt

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

Start Hunting!

Translated by