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:
Data Reading/Plotting: Matrix names as alpha-numeric filenames and auto-updating legend

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames and auto-updating legend

From: Stan

Date: 1 Jul, 2012 22:39:07

Message: 1 of 12

I am importing several data files and plotting data from these files on the graph (same x-values). The data consist of 2 columns - x-values in column 1 and y-values in column 2. Since there will be numerous data sets on the same graph, I must add a legend for the plots. However, I would like the legend to update automatically after more data sets to the graph. I do not know ahead of time how many data sets will be plotted on the same graph.

What I can do:
I can use fileparts to extract the alpha-numeric part of the filename (i.e. the part without the extension). This is the part that I must now use in the legend. My usage of fileparts:

files = dir('*.dat');
for k = 1:numel(files);
    file=files(k).name;
    [pathstr,name,ext] = fileparts(file);
    d=importdata(file);
    Matrix=d.data;
.
.
.
end

Problem 1:
1. ) Is there some way for me to dynamically generate the legend headings, from the filename (i.e. from the name variable)?

Problem 2:
I need to save each extracted data set to a matrix (n rows X 2 columns) as I extract it (from each file) and then horzcat() the n matrices (all n matrices will have the same size).
CONDITION 1: I am not sure how many data files will be processed so I do not know the number of matrices, k, that will be produced.
CONDITION 2: I would like to use this approach:

Each c{i} is a "matrix":

>> c={[1 2] [3] [4 5 6] [7 8]}

c =

    [1x2 double] [3] [1x3 double] [1x2 double]

>> horzcat(c{:})

ans =

     1 2 3 4 5 6 7 8

With the 2 conditions in mind, my questions are:

2. a) Is there some way for me to store the entire file name as the matrix name?
2. b) How would I add n matrices (which have the filename as their name) to a cell comma list (like shown above)? Or is there another way to horizontally concatenate n matrices?

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: dpb

Date: 2 Jul, 2012 16:45:43

Message: 2 of 12

On 7/1/2012 5:39 PM, Stan wrote:
> I am importing several data files and plotting data from these files on
...
> I can use fileparts to extract the alpha-numeric part of the filename
> (i.e. the part without the extension). This is the part that I must now
> use in the legend. My usage of fileparts:
>
> files = dir('*.dat');
> for k = 1:numel(files);
> file=files(k).name;
> [pathstr,name,ext] = fileparts(file);
> d=importdata(file);
> Matrix=d.data;
> ..
> ..
> ..
> end
>
> Problem 1:
> 1. ) Is there some way for me to dynamically generate the legend
> headings, from the filename (i.e. from the name variable)?

Sure...just use

files(k).name

as the text argument to legend() if that's what you want or use it or
appropriate substrings from it in conjunction with whatever else you
wish and build another specific string...

Or, you can do sotoo...

d = dir('*.dat');
f=d(1).name;
x=importdata(f);
[~,n] = fileparts(f));
l{1}=n;
plot(x(:,1),y(:,2))
hold on
for 2 = 1:numel(files)
   f=d(i).name;
   x=importdata(f);
   [~,n] = fileparts(f));
   l{i}=n;
   ...
   plot(x(:,1),y(:,2))
end
legend(l)

> Problem 2:
> I need to save each extracted data set to a matrix (n rows X 2 columns)
> as I extract it (from each file) and then horzcat() the n matrices (all
> n matrices will have the same size).
...

If they're all the same length, simply writing

z=[z x];

does it. Write the above w/ z as the first case to initialize it then
put the concatenation inside the loop.

Unless the data are very large the automagic "growing" of the array
won't be terribly noticeable. If it is, then preallocate an estimate
for the number of columns and address directly and check for overflow
and reallocate if needed.

Could be added as cells in a cell array simply by indexing w/ the {}
curly brackets.

--

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: Stan

Date: 4 Jul, 2012 21:21:07

Message: 3 of 12

Oh that's a very nice way to generate the matrix. Thanks for that.

Regarding the legend, I've made a bit of progress with the graph but am still having trouble. I think it has to do with what it is that is holding my legend entries. Here's the setup I am working with.
 
>> mat2=rand(2048,5);

>> numel(files)

ans =

     5

>> NNN

NNN =

    'block'
    'block2'
    'block3'
    'block4'
    'block5'

NNN is a 5 X 1 cell. The cell entries are what I must use as legend entries on my graph. This, I believe, is the source of my problems.

x=1:(a_setup*b_setup)-num_nan;
>> size(x)

ans =

           1 2048

x is a 1 X 2048 double. It is just a series of numbers from 1 to 2048. It can be generated as follows:
x = 1:2048; %given the dimensions of mat2, x' will have to be graphed

So:
There is a matrix mat2, with 5 columns. Each column represents a 'y' variable that I wish to plot against a common x, all (columns) on the same graph.

MY ATTEMPT AT THE LOOP FOR THE GRAPH:
for qqqra = 1:numel(files); %from 1 to 5
    bbbb(qqqra,1) = plot(x',mat2(:,qqqra),'LineWidth',2);
end

Explanation of the 2nd line of the loop:
- take x' as the x and qqqra (as the column number from the matrix mat2) as the y and plot them

QUESTION:
I need the legend to update its entries as graphs are added/removed. How do I now incorporate the legend (given that the entries are stored in NNN) so that I can dynamically add/remove legend entries to the plot as graphs are added/removed?

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: dpb

Date: 4 Jul, 2012 22:41:55

Message: 4 of 12

On 7/4/2012 4:21 PM, Stan wrote:
...
> ... Here's the setup I am working with.
>>> mat2=rand(2048,5);
>
...

> NNN =
> 'block'
> 'block2'
> 'block3'
> 'block4'
> 'block5'
>
> NNN is a 5 X 1 cell. The cell entries are what I must use as legend
> entries on my graph. This, I believe, is the source of my problems.

Surely don't see what that would be (yet, anyway...)

...

> x = 1:2048; %given the dimensions of mat2, x' will have to be graphed

In that case, plot(Y) will plot the columns of Y vs their index w/o any
X input at all...

doc plot

But also note that if you need a column vector, then write

x=[1:length(mat2)]';

and get the orientation you want/need from the git-go.

> So:
> There is a matrix mat2, with 5 columns. Each column represents a 'y'
> variable that I wish to plot against a common x, all (columns) on the
> same graph.

So, what's wrong w/

plot(x,mat2)
legend(NNN)

???

At the command line here, even using your variable names...

 >> mat2=rand(2048,5);
 >> l='';for i=1:5,l=strvcat(l,['Block ' num2str(i)]);end
 >> NNN=cellstr(l);
 >> plot(mat2)
 >> legend(NNN)
 >>

Given the nature of rand() data as random over the same range the
resulting plot is pretty much a solid bunch of squiggles but presuming
you have an actual data set of some other type in mind, it produces a
plot and legend...

> MY ATTEMPT AT THE LOOP FOR THE GRAPH:
> for qqqra = 1:numel(files);
> bbbb(qqqra,1) = plot(x',mat2(:,qqqra),'LineWidth',2);
> end
>
...

> QUESTION:
> I need the legend to update its entries as graphs are added/removed. How
> do I now incorporate the legend (given that the entries are stored in
> NNN) so that I can dynamically add/remove legend entries to the plot as
> graphs are added/removed?

Well given the length of the vector, maybe I can guess this is a
measured signal and the data will overlay pretty much and you want to
just watch them go by one at a time...

If so to do this sequentially,

for i=1:numel(files)
   plot(mat2(:,i))
   legend(NNN(i))
   pause(0.5)
end

Does that help?

BTW, if you want a color change from the default 'b' that plot() will
reset to each time, save the letter mnemonics of choice (as documented
in plot() ) in an array and pass one of them w/ an index in the modifier
triplet position.

--

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: Stan

Date: 4 Jul, 2012 23:30:08

Message: 5 of 12

> So, what's wrong w/
>
> plot(x,mat2)
> legend(NNN)
>
> ???
>

This worked.

> Well given the length of the vector, maybe I can guess this is a
> measured signal and the data will overlay pretty much and you want to
> just watch them go by one at a time...
>
> If so to do this sequentially,
>
> for i=1:numel(files)
> plot(mat2(:,i))
> legend(NNN(i))
> pause(0.5)
> end
>
> Does that help?

Thsi is actually what I was trying unsuccessfully. I tried this, without that pause(0.5) line. It said ``Ignoring extra legend entries`` and it only displayed one legend entry. Why would this approach not workÉ

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: dpb

Date: 5 Jul, 2012 03:03:03

Message: 6 of 12

On 7/4/2012 6:30 PM, Stan wrote:
...

>> If so to do this sequentially,
>>
>> for i=1:numel(files)
>> plot(mat2(:,i))
>> legend(NNN(i))
>> pause(0.5)
>> end
>>
>> Does that help?
>
> Thsi is actually what I was trying unsuccessfully. I tried this, without
> that pause(0.5) line. It said ``Ignoring extra legend entries`` and it
> only displayed one legend entry. Why would this approach not workÉ

The pause() has no bearing other than to slow it down enough you can see
it happen...otherwise you'll not see anything except the last as the
other will just have a flicker and then show the last plot.

If you got an error you did something other than just replicate the
above--what, would have to see the exact code to tell.

--

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: Steven_Lord

Date: 5 Jul, 2012 03:10:55

Message: 7 of 12



"Stan " <studentt_44@hotmail.com> wrote in message
news:jt2c43$s4j$1@newscl01ah.mathworks.com...
> Oh that's a very nice way to generate the matrix. Thanks for that.
>
> Regarding the legend, I've made a bit of progress with the graph but am
> still having trouble. I think it has to do with what it is that is holding
> my legend entries. Here's the setup I am working with.

*snip*

> QUESTION:
> I need the legend to update its entries as graphs are added/removed. How
> do I now incorporate the legend (given that the entries are stored in NNN)
> so that I can dynamically add/remove legend entries to the plot as graphs
> are added/removed?

One way to do this is to set the DisplayName property of the line when you
create it.

http://www.mathworks.com/help/techdoc/ref/line_props.html#DisplayName

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: Stan

Date: 5 Jul, 2012 03:13:06

Message: 8 of 12

> The pause() has no bearing other than to slow it down enough you can see
> it happen...otherwise you'll not see anything except the last as the
> other will just have a flicker and then show the last plot.
>
> If you got an error you did something other than just replicate the
> above--what, would have to see the exact code to tell.
>
> --

Here is what I tried:

for iii=1:numel(files)
plot(mat2(:,iii))
legend(NNN(iii))
end

I got just block5 ( the last entry in NNN) in the legend. I couldn't see the other entries. I think the plot is being over-written so I tried these 2:

for iii=1:numel(files)
a(iii)=plot(mat2(:,iii))
legend(a(iii),NNN{iii})
end

then

for iii=1:numel(files)
aa(iii,1)=plot(mat2(:,iii))
legend(aa(iii,1),NNN{iii})
end

but each of them did the same thing - only block5 ( the last entry was displayed in the legend).

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: dpb

Date: 5 Jul, 2012 03:42:37

Message: 9 of 12

On 7/4/2012 10:13 PM, Stan wrote:
>> The pause() has no bearing other than to slow it down enough you can
>> see it happen...otherwise you'll not see anything except the last as
>> the other will just have a flicker and then show the last plot.
>>
>> If you got an error you did something other than just replicate the
>> above--what, would have to see the exact code to tell.
>>
>> --
>
> Here is what I tried:
>
> for iii=1:numel(files)
> plot(mat2(:,iii))
> legend(NNN(iii))
> end
>
> I got just block5 ( the last entry in NNN) in the legend. I couldn't see
> the other entries. I think the plot is being over-written

Well, yes...that should be obvious when it was run w/ the pause() in
there...

> ... so I tried
> these 2:
>
> for iii=1:numel(files)
> a(iii)=plot(mat2(:,iii))
> legend(a(iii),NNN{iii})
> end

Same thing just using explicit handle...

>
> then
>
> for iii=1:numel(files)
> aa(iii,1)=plot(mat2(:,iii))
> legend(aa(iii,1),NNN{iii})
> end

Again, the same thing except changed to write a superfluous array index.

> but each of them did the same thing - only block5 ( the last entry was
> displayed in the legend).

If you don't want to overwrite the first, then you need to use 'hold on'
after the first...

plot(mat2(:,1))
legend(NNN(1))
hold on
for i=2:numel(files)
   plot(mat2(:,i))
   legend(NNN(i))
  end

That is, in the end, the same thing as the original

plot(mat2)
legend(NNN)

I see Steven has pointed out there's now a DisplayName property of the
line object that legend() uses instead of having to either use legend()
itself or to save the handles to the objects returned by legend() and
manipulate them manually. That's kinda' useful; you might want to look
into that as well.

Overall, I don't really know what effect it is that you're trying to
create. If you don't put a pause() or have some other mechanism going
on to slow down a loop the net effect of any of the loops is going to be
the same as the plot() on the matrix visually so there's where I'm
having trouble following where you think you're heading...

--

--

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: Stan

Date: 5 Jul, 2012 16:46:08

Message: 10 of 12

The

>> hold on

has worked. That is producing the effect that I wanted. Thanks a lot. As you said, it is the same as the first approach. I was not aware of the ability of plot() to plot an entire matrix as its y-variable. This is where my problem was occuring. Also, I didn't know that legend(NNN) was sufficient. I thought that individual legend entries had to be extracted and printed to the legend one at a time. Your posts, and further reading on my part, have cleared that up. It is very helpful to know this since I have to do this (automatically updating legend) a lot.

I have 2 related questions:
1. Regarding NNN
>> NNN =
>> 'block'
>> 'block2'
>> 'block3'
>> 'block4'
>> 'block5'

NNN is a 5 X 1 cell. I need to add 2 entries to the left (or top) of this cell. I need it to be:
>> NNN =
>> 'testing'
>> 'counter'
>> 'block'
>> 'block2'
>> 'block3'
>> 'block4'
>> 'block5'

This is not for purposes of the legend. This is for printing the 7 x 1 cell entries to an EXCEL file as column headers.

I have tried, with no luck, to convert NNN into a 1 column matrix and then add rows. Is there another way to accomplish this?

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: dpb

Date: 5 Jul, 2012 17:45:33

Message: 11 of 12

On 7/5/2012 11:46 AM, Stan wrote:
> The
>
>>> hold on
>
> has worked. That is producing the effect that I wanted. Thanks a lot. As
> you said, it is the same as the first approach. I was not aware of the
> ability of plot() to plot an entire matrix as its y-variable. This is
> where my problem was occuring. Also, I didn't know that legend(NNN) was
> sufficient. I thought that individual legend entries had to be extracted
> and printed to the legend one at a time. Your posts, and further reading
> on my part, have cleared that up. It is very helpful to know this since
> I have to do this (automatically updating legend) a lot.

I _might_ point out

doc plot % here if I were to be snarky, but I won't... :)

legend() adds whatever is passed to it as rows in a character string
array or as cell-strings. It automagically correlates the linestyle of
the nth line to the corresponding entry; if you want those to correspond
you have to pass the correct number of strings. You can make other
characteristics by judicious other choices.

In your case of plotting a column at a time, it's passing the one
corresponding string in conjunction w/ 'hold on' that makes it show up
as it does as all five in the end (the same as if use the array form for
plot and the full cell array at one call).
>
> I have 2 related questions:
> 1. Regarding NNN
...

> NNN is a 5 X 1 cell. I need to add 2 entries to the left (or top) of
> this cell. I need it to be:
>>> NNN =
>>> 'testing'
>>> 'counter'
>>> 'block'
>>> 'block2'
>>> 'block3'
>>> 'block4'
>>> 'block5'
>
...

> I have tried, with no luck, to convert NNN into a 1 column matrix and
> then add rows. Is there another way to accomplish this?

Well, you haven't shown what you tried and again it looks like you're
not using the doc's very effectively... :(

Remember from some time back I built NNN (and I really _do_ wish you'd
use meaningful variable naming; it would make things much easier to
remember what is what and surely make your code much more readable and
hence both more easily written as well as debugged. Not to mention
trying to maintain it for some length of time) that 'l' was a character
array we used cellstr() on to get the cellstring array....

 >> cellstr(strvcat('Some new stuff',l)) % using the character array
ans =
     'Some new stuff'
     'Block 1'
     'Block 2'
     'Block 3'
     'Block 4'
     'Block 5'
 >> cellstr(strvcat('Some new stuff',char(NNN))) % convert then go back
ans =
     'Some new stuff'
     'Block 1'
     'Block 2'
     'Block 3'
     'Block 4'
     'Block 5'
 >> cellstr(strvcat('Some new stuff',NNN))
??? Error using ==> char
Inputs must be character arrays.

Error in ==> C:\matlabR12\toolbox\matlab\strfun\strvcat.m
On line 23 ==> s = char(varargin{notempty});

 >>

The last is a response to the note I see in current doc that strvcat()
is to be removed in a future version and the functionality has been
placed in char(). Depending on your version I don't know when that
happened but the previous behavior is the latter.

doc cellstr
doc char % and follow the links to their friends and explore
help strings % overview
help strfun % see the complete menagerie

It would seem there really should be an 'strinsert' function or some
other way a little less verbose but afaict this is still it...

And the second question was????

--

Subject: Data Reading/Plotting: Matrix names as alpha-numeric filenames

From: Stan

Date: 6 Jul, 2012 04:06:08

Message: 12 of 12

Ah thanks. That was easy. char worked. I simply inserted as indicated and it did the same thing.

I had previously tried:
horzcat
vertcat
cell2str followed by horzcat

None of those were working.

Also, there was no other question in my previous post....just that one regarding NNN.

Thanks again for all the help. I think that this thread has been resolved.

Tags for 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