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:
Need best way to store two pieces of info with one index

Subject: Need best way to store two pieces of info with one index

From: Jeff

Date: 1 Feb, 2011 22:23:05

Message: 1 of 4

OK, that subject wasn't the best, but I couldn't think of a better one.

What I need is a good way to save two or more pieces of information for each occurrence of something (does that make any more sense than the subject?). To be more specific, I have sequences of frequencies extracted from an FFT. My prof wants me to extract the peaks and valleys *and* the times when they occur. So what I need is a good way to store two pieces of information (peak, timestep), or (valley, timestep) with one index. Do you get my lame ass description?

And I have to do the same for many sequences of frequencies.

What's the best way in Matlab to store that type of structure? Bear in mind that I don't know the Matlab Object Model and am not very skilled with OO type coding (not that I'm against learning MOM or OO coding). Also bear in mind that this might be ported to a large cluster and speed of execution could become important.

Right now, I'm (just starting the code) I'm maintaining four matrices. One matrix has the peaks, another has the peak times; the third and fourth list have the valley and valley times. I have to extract that information for many different frequency sequences.

Links to tutorials are appreciated, too.

Subject: Need best way to store two pieces of info with one index

From: ImageAnalyst

Date: 1 Feb, 2011 22:38:21

Message: 2 of 4

Just use an array of structures. For example,

k is the peak number, for example 1, 2, 3, etc.

strPeaks(k).PeakStart = 10; % or whatever
strPeaks(k).PeakEnd = 20;
strPeaks(k).PeakCenter = 15;
strPeaks(k).Time = now;
strPeaks(k).ValleyStart = 30; % or whatever
strPeaks(k).ValleyEnd = 50;
strPeaks(k).ValleyCenter = 40;

or whatever. You can just make up the structure name and field member
names as you go along.

Subject: Need best way to store two pieces of info with one index

From: Jeff

Date: 2 Feb, 2011 03:56:03

Message: 3 of 4

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <cdd4ef2f-c3e2-4f48-87d6-11e8296f1530@g1g2000prb.googlegroups.com>...
> Just use an array of structures. For example,
>
> k is the peak number, for example 1, 2, 3, etc.
>
> strPeaks(k).PeakStart = 10; % or whatever
> strPeaks(k).PeakEnd = 20;
> strPeaks(k).PeakCenter = 15;
> strPeaks(k).Time = now;
> strPeaks(k).ValleyStart = 30; % or whatever
> strPeaks(k).ValleyEnd = 50;
> strPeaks(k).ValleyCenter = 40;
>
> or whatever. You can just make up the structure name and field member
> names as you go along.

Perfect!
I found two good help documents, too. I'll have this tidied up in no time. :D

P.S. For my own reference:
http://www.mathworks.com/help/techdoc/ref/f16-42340.html#f16-53937
http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-38.html

Subject: Need best way to store two pieces of info with one index

From: Jeff

Date: 3 Feb, 2011 14:46:04

Message: 4 of 4

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <cdd4ef2f-c3e2-4f48-87d6-11e8296f1530@g1g2000prb.googlegroups.com>...
> Just use an array of structures. For example,
>
> k is the peak number, for example 1, 2, 3, etc.
>
> strPeaks(k).PeakStart = 10; % or whatever
> strPeaks(k).PeakEnd = 20;
> strPeaks(k).PeakCenter = 15;
> strPeaks(k).Time = now;
> strPeaks(k).ValleyStart = 30; % or whatever
> strPeaks(k).ValleyEnd = 50;
> strPeaks(k).ValleyCenter = 40;
>
> or whatever. You can just make up the structure name and field member
> names as you go along.

I read about structures (and a little bit about cell arrays). One thing is not clear, maybe you can help me finalize this.

I have several frequencies, each of which will have a list of max and mins, but they will not have the same amount. The best way to set this up (I think) is an array for each frequency, which then has an array of structure (code sample below). In the code below, is freq an array struct or a cell array? If I know the number of frequencies (e.g. nFreq=9), how do I preallocate freq?

I execute the commands below, then type 'whos freq'; it says freq is '1x2 struct'. I could not do 'whos freq.strPeaks', or 'whos freq.strPeaks(1)'. I tried 'freq=struct(nFreqs)', but got an error.

Thanks.

freq(1).strPeaks(1).PeakStart=10
freq(1).strPeaks(1).PeakEnd=20
freq(1).strPeaks(1).PeakCenter=15

freq(1).strPeaks(2).PeakStart=30
freq(1).strPeaks(2).PeakEnd=40
freq(1).strPeaks(2).PeakCenter=35
...
freq(1).strPeaks(number of freq 1 peaks).PeakStart=5
freq(1).strPeaks(number of freq 1 peaks).PeakEnd=5
freq(1).strPeaks(number of freq 1 peaks).PeakCenter=5
...
freq(2).strPeaks(1).PeakStart=10
freq(2).strPeaks(1).PeakEnd=20
freq(2).strPeaks(1).PeakCenter=15
...
freq(2).strPeaks(number of freq 2 peaks).PeakStart=3
freq(2).strPeaks(number of freq 2 peaks).PeakEnd=3
freq(2).strPeaks(number of freq 2 peaks).PeakCenter=3

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