Question about taking data from a .txt file

2 Ansichten (letzte 30 Tage)
Choke
Choke am 19 Jun. 2015
Kommentiert: Choke am 19 Jun. 2015
I have a .txt that will have a group of that I need to pull out based off of a space being in between. For example, a certain chunk will have a title, followed by an unknown number of lines that start with a hyphen, that will then be followed by a space.
How can I pull out each block of hyphens into its own unique cell? I want it to start at the first hyphen of the group and end at the next space. I know I need to use a loop but I'm having trouble setting this up.
I've tried setting up a combination of for/while loops while using if statements but I'm having no luck.
Any sort of help will be appreciated.
  4 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 19 Jun. 2015
This is not clear, Room 1 is in cell{1,1} or in cell{2,1}? can you just post the expected result for this example?
Choke
Choke am 19 Jun. 2015
Bearbeitet: Choke am 19 Jun. 2015
I don't have expected results because this is only the first part of a larger problem I'm having. (Edit: I know how to do everything in this particular problem after this step is solved. I just can't figure out how to put multiple line of a text file into a single cell)
The title 'Room 1' is in cell{1,1}. The actual contents in Room 1 (- 6 chairs - 5 tables - 2 lamps) needs to be in cell{2,1}. 'Room 2' will be in cell{1,2} while the '-' for Room 2 will be in cell {2,2} and so on.
Sorry, I can't be more specific, but like I said, this is the first of about five steps I need to do and I only have the final solution, on top of that, the examples I have to run are a lot more complicated so I'm trying to simplify it.
I really appreciate you taking your time.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 19 Jun. 2015
fid = fopen('fic.txt');
line1 = fgetl(fid);
k=0;
while ischar(line1)
k=k+1;
res{k,1}=line1;
line1 = fgetl(fid);
end
fclose(fid);
idx1=find(~cellfun(@isempty,regexpi(res,'room')))
idx2=[idx1(2:end)-1; numel(res)]
for k=1:numel(idx1)
out{1,k}=res{idx1(k)}
out{2,k}=res(idx1(k)+1:idx2(k))
end

Weitere Antworten (1)

Stephen23
Stephen23 am 19 Jun. 2015
Bearbeitet: Stephen23 am 19 Jun. 2015
There is no need to use any slow loops for this, when regexp does it all in one go:
fmt = '(^Room \d+)\s*\n(^- \d+ [^\n]+(\n|$))+';
str = fileread('temp.txt');
C = regexpi(str,fmt,'lineanchors','tokens');
C = vertcat(C{:});
and the output looks like this:
>> C
C =
'Room 1' [1x35 char]
'Room 2' [1x49 char]
'Room 3' [1x43 char]
>> C{1,2}
ans =
- 6 chairs
- 5 tables
- 2 lamps
And of course you can do something similar with the names in the second column, if you wish to split them into separate cells. I used my FEX submission to help generate the regexp regular expression:
And this is the file that I used:
  1 Kommentar
Choke
Choke am 19 Jun. 2015
Thanks, that got me what I needed, but I'm not comfortable using regexp yet. Haven't learned it. But thanks again.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by