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:
Help with strtok

Subject: Help with strtok

From: Kevin

Date: 3 Feb, 2011 07:33:05

Message: 1 of 7

Alright, I'm taking MATLAB this semester in college, and this is the first time I have ever learned a new programming language.

My professors and TA's are utter garbage and in no way are helpful for me learning this language. The homework is impossible, and google is more helpful than lecture.

So one of the questions is, I start out with a string that is seperated by commas, such as

'well,hate,this,class'

and I have to use strtok to store the first word into one variable, say A, and the rest into another.

Then I have to store the second value into another variable and the rest into another.

I've looked all over to find how to do this, but I can't find anything. I've been trying to solve this one problem for the past two hours.

If anyone, please, help me. I'm about to go insane over this stupid class.

Subject: Help with strtok

From: John D'Errico

Date: 3 Feb, 2011 11:46:07

Message: 2 of 7

"Kevin " <kevin_Spartan17@yahoo.com> wrote in message <iidlnh$dd0$1@fred.mathworks.com>...
> Alright, I'm taking MATLAB this semester in college, and this is the first time I have ever learned a new programming language.
>
> My professors and TA's are utter garbage and in no way are helpful for me learning this language. The homework is impossible, and google is more helpful than lecture.
>
> So one of the questions is, I start out with a string that is seperated by commas, such as
>
> 'well,hate,this,class'
>
> and I have to use strtok to store the first word into one variable, say A, and the rest into another.
>
> Then I have to store the second value into another variable and the rest into another.
>
> I've looked all over to find how to do this, but I can't find anything. I've been trying to solve this one problem for the past two hours.
>
> If anyone, please, help me. I'm about to go insane over this stupid class.

I'd just give up if I were you, take a basket weaving class
instead. MATLAB is obviously impossible to learn, and you
have apparently given up already, with no interest in the
class.

The fact is, strtok does exactly what you want. You might
consider using a loop, maybe even a cell array?

Perhaps you need to try a different attitude.

Subject: Help with strtok

From: Richard Crozier

Date: 3 Feb, 2011 12:51:04

Message: 3 of 7

"Kevin " <kevin_Spartan17@yahoo.com> wrote in message <iidlnh$dd0$1@fred.mathworks.com>...
> Alright, I'm taking MATLAB this semester in college, and this is the first time I have ever learned a new programming language.
>
> My professors and TA's are utter garbage and in no way are helpful for me learning this language. The homework is impossible, and google is more helpful than lecture.
>
> So one of the questions is, I start out with a string that is seperated by commas, such as
>
> 'well,hate,this,class'
>
> and I have to use strtok to store the first word into one variable, say A, and the rest into another.
>
> Then I have to store the second value into another variable and the rest into another.
>
> I've looked all over to find how to do this, but I can't find anything. I've been trying to solve this one problem for the past two hours.
>
> If anyone, please, help me. I'm about to go insane over this stupid class.

Kevin, I am going to help you, but first:

Trust me when I say I have felt the same way as you occasionally, but also trust me when I say that the way to get the best out of people on a forum is not to go in all guns blazing slagging off your teachers, who may or may not be any good. Some people on this forum are smart and will have taught themselves from scratch with no teachers at all. So man up and calm down before posting.

Why don't you try something like:

isfirstrun=true;str='I,need,to,chill,out'
word={};count=1;
while isfirstrun || ~isempty(str),isfirstrun=false;[word{count},str]=strtok(str,',')
count=count + 2;end

word is a Cell Array, look that up. You'll have figure out the rest of what's going on for yourself. Try using the Matlab help system for instance? There is also a deliberate error here you will have to fix. You might also want to fix the formatting to make it more readable, I have deliberately done this badly.

Subject: Help with strtok

From: Bjorn Gustavsson

Date: 3 Feb, 2011 13:14:03

Message: 4 of 7

"Kevin " <kevin_Spartan17@yahoo.com> wrote in message <iidlnh$dd0$1@fred.mathworks.com>...
> Alright, I'm taking MATLAB this semester in college, and this is the first time I have ever learned a new programming language.
>
> My professors and TA's are utter garbage and in no way are helpful for me learning this language. The homework is impossible, and google is more helpful than lecture.
>
> So one of the questions is, I start out with a string that is seperated by commas, such as
>
> 'well,hate,this,class'
>
> and I have to use strtok to store the first word into one variable, say A, and the rest into another.
>
> Then I have to store the second value into another variable and the rest into another.
>
> I've looked all over to find how to do this, but I can't find anything. I've been trying to solve this one problem for the past two hours.
>
> If anyone, please, help me. I'm about to go insane over this stupid class.
>
To add a suggestion (or two): Go through all the matlab demos! At the matlab prompt type: demo, then browse away! It might take a while, you might not find everything directly applicable to the current home-work task, but you should be able to find useful stuff in there, and with some judgement you'll also find what you need. In addition this approach will give you a broad oeverview of matlab, and you'll see a lot of good programming pattern - and avoid learning some poor habits. Then learn to browse the matlab help, that is an equally usefull skill to learn.

HTH,
Bjoern

Subject: Help with strtok

From: Jan Simon

Date: 3 Feb, 2011 13:39:03

Message: 5 of 7

Dear Kevin,

It seems like you are not in a good shape today.

> My professors and TA's are utter garbage and in no way are helpful for me learning this language. The homework is impossible, and google is more helpful than lecture.

If someone hits a book on the head, and it make an amphoric tone, this is not necessarily caused by the book.
 
> I've looked all over to find how to do this, but I can't find anything.
Search more specific: The homework concerns STRTOK, so ask Google for the tern "Matlab strtok". The first hit is the techdoc of MathWorks:
  http://www.mathworks.com/help/techdoc/ref/strtok.html
There, or with "doc strtok" or "help strtok", you find all details you need.
 
> If anyone, please, help me. I'm about to go insane over this stupid class.
Going insane is a valuable solution also. Be aware, that after this trivial STRTOK question things like these are waiting for destroying you brain:

M = sort(accumarray(sample,sample(:,2),[],@(x)x,[],true),2,'descend');
results = full(sortrows([repmat([1:size(M,1)].',2,1), M(1:size(M,1)*2).']))

s = sortrows(sample, [1, -2]);
q = ~[0 ; ~diff(s(:,1))];
q2 = q | [true; q(1:end-1)];
Result = s(q2,:)

STR(cell2mat(cellfun(@(x)x(1)-diff(x)-1:x(2),regexp(STR,'(<)+','tokenExtents'),'Un',0))) = ''

(Thanks Jos, Sean and Us for these great examples to show frustrated beginners, that the univers is big)

Kind regards, Jan

Subject: Help with strtok

From: Kevin

Date: 3 Feb, 2011 16:46:04

Message: 6 of 7

"Jan Simon" wrote in message <iieb5n$o8g$1@fred.mathworks.com>...
> Dear Kevin,
>
> It seems like you are not in a good shape today.
>
> > My professors and TA's are utter garbage and in no way are helpful for me learning this language. The homework is impossible, and google is more helpful than lecture.
>
> If someone hits a book on the head, and it make an amphoric tone, this is not necessarily caused by the book.
>
> > I've looked all over to find how to do this, but I can't find anything.
> Search more specific: The homework concerns STRTOK, so ask Google for the tern "Matlab strtok". The first hit is the techdoc of MathWorks:
> http://www.mathworks.com/help/techdoc/ref/strtok.html
> There, or with "doc strtok" or "help strtok", you find all details you need.
>
> > If anyone, please, help me. I'm about to go insane over this stupid class.
> Going insane is a valuable solution also. Be aware, that after this trivial STRTOK question things like these are waiting for destroying you brain:
>
> M = sort(accumarray(sample,sample(:,2),[],@(x)x,[],true),2,'descend');
> results = full(sortrows([repmat([1:size(M,1)].',2,1), M(1:size(M,1)*2).']))
>
> s = sortrows(sample, [1, -2]);
> q = ~[0 ; ~diff(s(:,1))];
> q2 = q | [true; q(1:end-1)];
> Result = s(q2,:)
>
> STR(cell2mat(cellfun(@(x)x(1)-diff(x)-1:x(2),regexp(STR,'(<)+','tokenExtents'),'Un',0))) = ''
>
> (Thanks Jos, Sean and Us for these great examples to show frustrated beginners, that the univers is big)
>
> Kind regards, Jan

Alright, I do apologize for my earlier comments. I was a bit overworked yesterday, as I had homework for eight hours straight, and spend six on this one, and was not even a sixth of the way done. This was my last resort to posting, as I couldn't ask for help at two o clock in the morning.

I don't think we're allowed to use cell arrays or loops though, since that's a completely different section

Subject: Help with strtok

From: Jan Simon

Date: 3 Feb, 2011 20:47:03

Message: 7 of 7

Dear Kevin,

> I don't think we're allowed to use cell arrays or loops though, since that's a completely different section

Well, homework with forbidden commands is my job!
But unfortunately you do not need a cell array at all to solve the question. Actually you just have to copy a single line from the examples of the help text twice. That's all.

If I post the solution here, your professor will not be amused. You can be sure, that the assisents read CSSM and recognize the question, the solution, the copy&paste tricks and of course you, even if yur name is not Kevin, but Roberta. Most of all they'll recognize you, if you cannot solve the question! And because this is usenet, they will recognize you in 10 years also... As well as my employer will see, that I have time to care about STRTOK in the evening.

Kind regards, Jan

Tags for this Thread

No tags are associated with 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