Thread Subject: matlab random object

Subject: matlab random object

From: ching l

Date: 11 Jul, 2008 14:32:02

Message: 1 of 21

does rand in matlab only applicable for numbers?

What can I do if I want to randomise the files?

for examples:

wavread (file1);
wavread (file2);
wavread (file3);

wavplay rand(file); - the wavplay randomly choose from the
                       wavread

is it possible to do that?

Thanks in advance.


Subject: matlab random object

From: us

Date: 11 Jul, 2008 15:40:19

Message: 2 of 21

"ching l":
<SNIP wants to randomize the rest of the world...

one of the solutions

% the data
     lst={
          'unique.m'
          'whos.m'
          'figure.m'
     };
% the engine
% - randomly select a file for editing...
     ix=ceil(size(lst,1)*rand);
     edit(lst{ix});

us

Subject: matlab random object

From: ching l

Date: 11 Jul, 2008 17:05:26

Message: 3 of 21

I'm so sorry, but that's not very clear...

I'm quite new in matlab, basically, I need to make sure the
file can be played randomly when i click the Play button in
the gui........

possible to do that?


Subject: matlab random object

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Jul, 2008 17:13:39

Message: 4 of 21

In article <g583sm$bsc$1@fred.mathworks.com>,
ching l <chinglnc@hotmail.com> wrote:
>I'm so sorry, but that's not very clear...

>I'm quite new in matlab, basically, I need to make sure the
>file can be played randomly when i click the Play button in
>the gui........

>possible to do that?

[data, speed] = wavread(file1);
samples{1} = {data, speed};
[data, speed] = wavread(file2);
samples{2} = {data, speed};
[data, speed] = wavread(file3);
samples{3} = {data, speed};

uicontrol('Style','pushbutton','String','Play Randomly', ...
  'Units', 'normal', 'Position', [1/2 1/2 1/4 1/4], ...
  'Callback', @(src, evt) wavplay(samples{ceil(length(samples)*rand)}{:}) );

--
  "Beauty, like all other qualities presented to human experience,
  is relative; and the definition of it becomes unmeaning and
  useless in proportion to its abstractness." -- Walter Pater

Subject: matlab random object

From: ching l

Date: 11 Jul, 2008 18:34:01

Message: 5 of 21

cheers for that * crying with tears*..

can i have a couple of basic question please?

I've always seen these dots ... , what are these dots mean?

and what is {:} means?

Sorry, I couldn't find them in the matlab help.

Thanks a lot.

Subject: matlab random object

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Jul, 2008 18:59:57

Message: 6 of 21

In article <g5892p$9q7$1@fred.mathworks.com>,
ching l <chinglnc@hotmail.com> wrote:

>can i have a couple of basic question please?

>I've always seen these dots ... , what are these dots mean?

In the matlab help, click on "Functions -- By Category",
and once there, "Programming and Data Types", then
"Operators and Special Characters" from there, and then
the "Special characters" section of that. The ... will be
listed there with a link; if you click on that you will get
a more extensive writeup.

Alternately, you could just enter the search string
special characters
in the matlab help search box; the longer writeup is in
the first result there.

... Continuation. Three or more periods at the end of a line continue
    the current function on the next line. Three or more periods before
    the end of a line cause MATLAB to ignore the remaining text on the
    current line and continue the function on the next line. This
    effectively makes a comment out of anything on the current line
    that follows the three periods. See Entering Long Statements (Line
    Continuation) for more information.


>and what is {:} means?

In the code I showed, I used the trick documented in the "Cell Arrays"
documentation:


  Replacing Lists of Variables with Cell Arrays

  Cell arrays can replace comma-separated lists of MATLAB variables in
  o Function input lists
  o Function output lists
  o Display operations
  o Array constructions (square brackets and curly braces)

  If you use the colon to index multiple cells in conjunction with
  the curly brace notation, MATLAB treats the contents of each cell
  as a separate variable. For example, assume you have a cell array
  T where each cell contains a separate vector. The expression
  T{1:5} is equivalent to a comma-separated list of the vectors in
  the first five cells of T.


The syntax {:} is the same as {1:end} -- that is, indexing all
of the cell array contents, and creating a comma-seperated list
out of the result. In my code, I constructed each cell member
to have two elements, the first being the list of samples, and the
second being the sample frequency Fs returned by wavplay.
Using {:} on the cell array in the call to wavplay was equivilent
to having given wavplay two separate parameters, one for the data
samples and the other for the sampling frequency.
We don't know that the sampling frequency was the same for each
of the .wav files, so if we were to play them back all with the same
sampling frequency, some of them might have been at the wrong speed,
so we have to remember the sampling frequency for each file and
tell wavplay to use that frequency.

I could have used two different arrays to store the samples and the
frequencies, but then I would have had to temporarily store the
random number in order to use it to index both arrays at the same
offset; I would have had to have used a second anonymous function
to handle that situation, or else would have to have used a non-
anonymous function (anonymous functions cannot construct local variables.)
--
'Roberson' is my family name; my given name is 'Walter'.

Subject: matlab random object

From: ching l

Date: 11 Jul, 2008 19:03:02

Message: 7 of 21

cheers for that * crying with tears*..

can i have a couple of basic question please?

I've always seen these dots ... , what are these dots mean?

and what is {:} means?

Sorry, I couldn't find them in the matlab help.

Thanks a lot.

Subject: matlab random object

From: Dave Bell

Date: 11 Jul, 2008 19:05:49

Message: 8 of 21

ching l wrote:
> cheers for that * crying with tears*..
>
> can i have a couple of basic question please?
>
> I've always seen these dots ... , what are these dots mean?

It means the line extends, and is wrapped at that point.
When you copy into the editor, you can leave out the dots AND the
carriage return after them. It makes the code easier to read here, with
short lines.


> and what is {:} means?

"All elements in this dimension"
For example,

a = [1,2; 3,4];
gives you a 2x2 array or matrix,
1 2
3 4

a(1,:) gives you the first row only, or 1, 2.

a(2,:) would give you the second row, or 3, 4.

a(:,1) gives you the first column,
1
3

a(:,2) gives you the second column,
2
4

Dave

Subject: matlab random object

From: ching l

Date: 11 Jul, 2008 19:21:01

Message: 9 of 21

thanks walter..

it's much more clear now...but I still got a little
confusion about this- @(src, evt)

have tried the search, it doesn't specifically explain what
is it for....



Subject: matlab random object

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Jul, 2008 19:26:00

Message: 10 of 21

In article <gaOdk.10360$LG4.6975@nlpi065.nbdc.sbc.com>,
Dave Bell <dbell@TheSPAMFREEBells.net> wrote:
>ching l wrote:

>> and what is {:} means?

>"All elements in this dimension"

Not quite.

If you have an array A, then A(:) means the same as reshape(A,[],1)
-- that is, the values of A all considered together as a single column
vector. This is not the same as A(1:end) or A(1:length(A))
as those forms will result in a row vector if A is not a column vector.

Similarily, if C is a multidimensional cell array, C{:} means
the same as forming a comma seperated list of all the contents
of the cell column array reshape(C,[],1)

The "All elements in this dimension" meaning only applies if
at least two dimensions are given in the indexing. For example,
A(:,:) is the same shape as A, but A(:) is always a column vector.
--
  "The quirks and arbitrariness we observe force us to the
  conclusion that ours is not the only universe." -- Walter Kistler

Subject: matlab random object

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 11 Jul, 2008 19:38:39

Message: 11 of 21

In article <g58bqt$8aa$1@fred.mathworks.com>,
ching l <chinglnc@hotmail.com> wrote:

>it's much more clear now...but I still got a little
>confusion about this- @(src, evt)

>have tried the search, it doesn't specifically explain what
>is it for....

In the matlab help box, search for the key
function handle callbacks

The second result,

  Function Handle Callbacks Handle Graphics

is the one you want. In particular, in the second section
"Function Handle Syntax",

  In Handle Graphics, functions that you want to use as function
  handle callbacks must define at least two input arguments in the
  function definition:

  The handle of the object generating the callback (the source of
  the event)

  The event data structure (can be empty for some callbacks)

  MATLAB passes these two arguments implicitly whenever the
  callback executes.


--
  "There is nothing so bad but it can masquerade as moral."
                                              -- Walter Lippmann

Subject: matlab random object

From: ching l

Date: 11 Jul, 2008 19:46:02

Message: 12 of 21

I've a little bit of problem here, I started this by using
the Gui interface...and I'm not sure how can I move around
the boxes with your codes...they should be quite similar
though,

I created the Play button using the Push button in Gui
Interface, and in the M-File, under the Play function, I did
this..


function select_audio_Callback(hObject, eventdata, handles)

[audio, fs] = wavread('F:\matlab_gui\speech');
samples{1} = {audio, fs};
[audio, fs] = wavread('F:\matlab_gui\gtr');
samples{2} = {audio, fs};

wavplay(samples{ceil(length(samples)*rand)});


I thought it should be quite similar with your concept, but
it doesn't work...it works OK if I just put
wavplay (audio, 44100) though...


What did I miss out?

Subject: matlab random object

From: ching l

Date: 11 Jul, 2008 19:55:05

Message: 13 of 21

Ahh...sorry..

I ve spotted the mistakes!!

Thanks walter!!!

Subject: matlab random object

From: ching l

Date: 17 Jul, 2008 09:58:02

Message: 14 of 21

Is it possible to know the value of the rand?

Because I need to generate the results later, with its
corresponded audio.

for example something like this:

wavplay(samples{ceil(length(samples)*rand)}{:}) ;

csvwrite('subject1.dat',[(value of the rand);00s]);

Is it possible to do that?



Subject: matlab random object

From: Titus

Date: 17 Jul, 2008 11:22:02

Message: 15 of 21


"ching l" <chinglnc@hotmail.com> schrieb im Newsbeitrag
news:g5n53a$nja$1@fred.mathworks.com...
> Is it possible to know the value of the rand?
>
> Because I need to generate the results later, with its
> corresponded audio.
>
> for example something like this:
>
> wavplay(samples{ceil(length(samples)*rand)}{:}) ;
>
> csvwrite('subject1.dat',[(value of the rand);00s]);
>
> Is it possible to do that?
>
>
>

Hi,
just for the rand, not looking at the rest of the code. What about

randValue = rand;
wavplay(samples{ceil(length(samples)*randValue)}{:}) ;
csvwrite('subject1.dat',[randValue;00s]);

Titus


Subject: matlab random object

From: ching l

Date: 17 Jul, 2008 11:57:01

Message: 16 of 21

rand is a function, it can't be replaced with randValue...

matlab doesn't recognise it as function anymore..

Subject: matlab random object

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 17 Jul, 2008 14:14:29

Message: 17 of 21

In article <g5nc2d$srs$1@fred.mathworks.com>,
ching l <chinglnc@hotmail.com> wrote:

It is better if you quote context so that people know what you
are talking about. Please recall that the Matlabcentral reader
is merely -one- interface to the discussion, and 40% of the people
reading the discussion are doing so through interfaces that are
*not* graphical and do *not* show previous material in the thread
at the same time.


>rand is a function, it can't be replaced with randValue...
>matlab doesn't recognise it as function anymore..

I believe you missed the statement,

randValue = rand;

That calls rand once and saves the value generated in the
variable randValue.
--
  "There's no term to the work of a scientist." -- Walter Reisch

Subject: matlab random object

From: ching l

Date: 17 Jul, 2008 15:14:04

Message: 18 of 21

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g5nk44$qrj$1@canopus.cc.umanitoba.ca>...
> In article <g5nc2d$srs$1@fred.mathworks.com>,
> ching l <chinglnc@hotmail.com> wrote:
>
> It is better if you quote context so that people know what you
> are talking about. Please recall that the Matlabcentral
reader
> is merely -one- interface to the discussion, and 40% of
the people
> reading the discussion are doing so through interfaces
that are
> *not* graphical and do *not* show previous material in the
thread
> at the same time.
>
>
> >rand is a function, it can't be replaced with randValue...
> >matlab doesn't recognise it as function anymore..
>
> I believe you missed the statement,
>
> randValue = rand;
>
> That calls rand once and saves the value generated in the
> variable randValue.
> --
> "There's no term to the work of a scientist." -- Walter
Reisch



Thanks Walter, I didn't miss that statement, rather I put it
in different callback, which was the cause of the error.

How do I grab the value from the different callback?
I know this is no right....

function select_audio_Callback(hObject, eventdata, handles)
handles.randValue = rand;


function answer_faster_Callback(hObject, eventdata, handles)
csvwrite('subject1.dat',[handles.randValue;00]);

Subject: matlab random object

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 17 Jul, 2008 16:50:36

Message: 19 of 21

In article <g5nnjs$ic4$1@fred.mathworks.com>,
ching l <chinglnc@hotmail.com> wrote:

>function select_audio_Callback(hObject, eventdata, handles)
>handles.randValue = rand;

After that statement, you need

guidata(hObject,handles);


>function answer_faster_Callback(hObject, eventdata, handles)
>csvwrite('subject1.dat',[handles.randValue;00]);

However, if select_audio_Callback and answer_faster_Callback
are not both callbacks to the same object, then this is unlikely
to be satisfactory.

Go into the help documentation and in the main Search for: window,
enter the keywords: sharing data
and read either of the first two results (the two are very similar but
the second is more specific to GUIDE.)

--
  "Not the fruit of experience, but experience itself, is the end."
                                              -- Walter Pater

Subject: matlab random object

From: ching l

Date: 18 Jul, 2008 01:02:01

Message: 20 of 21

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g5nt8s$ajo$1@canopus.cc.umanitoba.ca>...
> In article <g5nnjs$ic4$1@fred.mathworks.com>,
> ching l <chinglnc@hotmail.com> wrote:
>
> >function select_audio_Callback(hObject, eventdata, handles)
> >handles.randValue = rand;
>
> After that statement, you need
>
> guidata(hObject,handles);
>
>
> >function answer_faster_Callback(hObject, eventdata, handles)
> >csvwrite('subject1.dat',[handles.randValue;00]);
>
> However, if select_audio_Callback and answer_faster_Callback
> are not both callbacks to the same object, then this is
unlikely
> to be satisfactory.
>
> Go into the help documentation and in the main Search for:
window,
> enter the keywords: sharing data
> and read either of the first two results (the two are very
similar but
> the second is more specific to GUIDE.)
>
> --
> "Not the fruit of experience, but experience itself, is
the end."
> -- Walter Pater

'handles' seems the only structure that can be used to store
data for use by different functions a GUI. I'm not really
sharing the field/object between different gui callback
functions, but only the randvalue (rand) in the wavplay
function, which is not belong to gui.

any idea how?







Subject: matlab random object

From: ching l

Date: 19 Jul, 2008 22:06:02

Message: 21 of 21

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g584c3$17v$1@canopus.cc.umanitoba.ca>...
> In article <g583sm$bsc$1@fred.mathworks.com>,
> ching l <chinglnc@hotmail.com> wrote:
> >I'm so sorry, but that's not very clear...
>
> >I'm quite new in matlab, basically, I need to make sure the
> >file can be played randomly when i click the Play button in
> >the gui........
>
> >possible to do that?
>
> [data, speed] = wavread(file1);
> samples{1} = {data, speed};
> [data, speed] = wavread(file2);
> samples{2} = {data, speed};
> [data, speed] = wavread(file3);
> samples{3} = {data, speed};

wavplay(samples{ceil(length(samples)*randValue)}{:}) ;


I have a question, I think I might have understand the codes
wrongly.

I need to get this number, e.g. samples{3}, the 3 in another
callback function so that I know which audio is played.

which one of the codes is representing the sample{}?

is it this one -> samples{ceil(length(samples)*randValue





Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
logical indexing us 11 Jul, 2008 11:42:19
ceil us 11 Jul, 2008 11:40:22
rand us 11 Jul, 2008 11:40:22
edit us 11 Jul, 2008 11:40:22
code us 11 Jul, 2008 11:40:22
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com