Select specific elements in a cell

Hello, this is a very frustrating problem for me
I am using a loop that reads text lines from a data file using fgetl , such a line could look like this (there are many lines):
60 233 24 53 26 53 234 32
And could continue forever. I am trying to create a vector of every 1st value and a separate vector for every 2nd value, which is possible, but the following elements come in pairs . To illustrate:
aline = a b e f e f e f e f e f e f e f ...
anotherline = a b e f e f e f e f e f e f ...
so on ...
I am trying to make a cell for the e and a separate cell for f values, but cannot 'select' them in order to do so. How can you do this? I have tried playing with rem(1:len,2) == 1 (len = length of vector) to select every "odd" or "even" number, but it doesn't work as u want it to, as you cannot do the following.
x = rem(1:25,2) == 0;
vector(x);
What am i missing?

6 Comments

Make it easy for us to help you by attaching your data file.
And be more precise in the explanation of what it is you're selection crietria are--is it the odd or even values by position or is it the value of the content?
You say you cannot do the following
x = rem(1:25,2) == 0; vector(x);
And, I'd say, "why can't you?" It's certainly the hard way to generate the index as
ix=2:2:25;
x=vector(ix);
or simply
x=vector([2:2:25]);
works just fine if you simply fscanf the input.
As IA says, a specific example would help a lot...if it's just a regular space-delimited file then probably the easiest is to read it all in and then select and return the desired columns from memory.
How ironic, i wrote that to make it easy to understand. When i look on it a second time, now it looks more like an assignment that a question, sorry for that :b
Anyway, i solved the problem like this (this is not the whole code, but just to show others who might have the same problem the principle in the solution):
fileLine = fgetl(fid); % gets a line of text
i = 1; % counts each time line
while ischar(fileLine)
numLine = str2num(fileLine); % text line converted to vector
WW{i} = numLine(1); % define W
DD{i} = numLine(2); % define D
for k = 1:floor(length(numLine)-2)/2
tt{k} = numLine(1+2*k);
CC{k} = numLine(2+2*k);
end
end
And then it reads a new line and i = i+1. The original code was so messy and big that i will spare you the details. Thanks for your time IA-superman and dpb!
Edit: by the way, how do i "solve" this question? I cannot 'accept answer' my own question it seems. Any need to?
for k = 1:floor(length(numLine)-2)/2
tt{k} = numLine(1+2*k);
CC{k} = numLine(2+2*k);
end
Is more efficiently written as outlined previously as
tt=numLine(3:2:end);
CC=numLine(4:2:end);
or
[tt CC]=reshape(numLine(3:end),2,[]).';
Indeed, one of the rules of Answers Forum is that can't accept own; that would allow for manipulating the scoring too easily...
dpb, you can accept your own answer you just don't get reputation points for it. He can't accept the answer because it's in the comments not the answer box.
Oh, ok...I am now informed on the "ACCEPT" rules--was "underneath the impression" couldn't actually accept one's own.
I added a full solution w/o the line-by-line loop as an additional answer that should solve the problem--if not, hopefully Kenan will come back and explain where there's a problem needing fixup.

Sign in to comment.

 Accepted Answer

dpb
dpb on 25 Jun 2014
Edited: dpb on 25 Jun 2014
Instead of reading the file line-by-line and "growing" the arrays incrementally, try something like
x=textread('filename');
WW=x(:,1);
DD=x(:,2);
tt=x(:,3:2:end);
CC=x(:,4:2:end);

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Asked:

on 24 Jun 2014

Edited:

dpb
on 25 Jun 2014

Community Treasure Hunt

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

Start Hunting!