Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Mathematics
General TopicsResearchOperations ResearchStatisticsMathematical LogicNumerical AnalysisUndergraduate MathAlgebra HelpRecreational Math
Math Software
MapleMathematicaMATLABScilabSASSPSS

Math Forum / Math Software / MATLAB / July 2008



Tip: Looking for answers? Try searching our database.

matlab random object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ching l - 11 Jul 2008 15:32 GMT
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.
us - 11 Jul 2008 16:40 GMT
"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
ching l - 11 Jul 2008 18:05 GMT
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?
Walter Roberson - 11 Jul 2008 18:13 GMT
>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)}{:}) );

Signature

 "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

ching l - 11 Jul 2008 19:34 GMT
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.
Walter Roberson - 11 Jul 2008 19:59 GMT
>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.)
Signature

'Roberson' is my family name; my given name is 'Walter'.

ching l - 11 Jul 2008 20:21 GMT
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....
Walter Roberson - 11 Jul 2008 20:38 GMT
>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.

Signature

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

Dave Bell - 11 Jul 2008 20:05 GMT
> 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
Walter Roberson - 11 Jul 2008 20:26 GMT
>> 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.
Signature

 "The quirks and arbitrariness we observe force us to the
 conclusion that ours is not the only universe." -- Walter Kistler

ching l - 11 Jul 2008 20:46 GMT
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?
ching l - 11 Jul 2008 20:55 GMT
Ahh...sorry..

I ve spotted the mistakes!!

Thanks walter!!!
ching l - 17 Jul 2008 10:58 GMT
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?
Titus - 17 Jul 2008 12:22 GMT
> Is it possible to know the value of the rand?
>
[quoted text clipped - 8 lines]
>
> 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
ching l - 17 Jul 2008 12:57 GMT
rand is a function, it can't be replaced with randValue...

matlab doesn't recognise it as function anymore..
Walter Roberson - 17 Jul 2008 15:14 GMT
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.
Signature

 "There's no term to the work of a scientist." -- Walter Reisch

ching l - 17 Jul 2008 16:14 GMT
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g5nk44$qrj$1@canopus.cc.umanitoba.ca>...

> It is better if you quote context so that people know what you
> are talking about. Please recall that the Matlabcentral reader
[quoted text clipped - 12 lines]
> That calls rand once and saves the value generated in the
> variable randValue.

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]);
Walter Roberson - 17 Jul 2008 17:50 GMT
>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.)

Signature

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

ching l - 18 Jul 2008 02:02 GMT
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g5nt8s$ajo$1@canopus.cc.umanitoba.ca>...

> >function select_audio_Callback(hObject, eventdata, handles)
> >handles.randValue = rand;
[quoted text clipped - 14 lines]
> and read either of the first two results (the two are very similar but
> the second is more specific to GUIDE.)

'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?
ching l - 11 Jul 2008 20:03 GMT
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.
ching l - 19 Jul 2008 23:06 GMT
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g584c3$17v$1@canopus.cc.umanitoba.ca>...
> >I'm so sorry, but that's not very clear...
>
[quoted text clipped - 10 lines]
> [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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.