http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473
MATLAB Central Newsreader  Data Reading/Plotting: Matrix names as alphanumeric filenames and autoupdating legend
Feed for thread: Data Reading/Plotting: Matrix names as alphanumeric filenames and autoupdating legend
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 01 Jul 2012 22:39:07 +0000
Data Reading/Plotting: Matrix names as alphanumeric filenames and autoupdating legend
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#881599
Stan
I am importing several data files and plotting data from these files on the graph (same xvalues). The data consist of 2 columns  xvalues in column 1 and yvalues 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.<br>
<br>
What I can do:<br>
I can use fileparts to extract the alphanumeric 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:<br>
<br>
files = dir('*.dat');<br>
for k = 1:numel(files);<br>
file=files(k).name;<br>
[pathstr,name,ext] = fileparts(file);<br>
d=importdata(file);<br>
Matrix=d.data;<br>
.<br>
.<br>
.<br>
end<br>
<br>
Problem 1:<br>
1. ) Is there some way for me to dynamically generate the legend headings, from the filename (i.e. from the name variable)?<br>
<br>
Problem 2:<br>
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).<br>
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.<br>
CONDITION 2: I would like to use this approach:<br>
<br>
Each c{i} is a "matrix":<br>
<br>
>> c={[1 2] [3] [4 5 6] [7 8]}<br>
<br>
c = <br>
<br>
[1x2 double] [3] [1x3 double] [1x2 double]<br>
<br>
>> horzcat(c{:})<br>
<br>
ans =<br>
<br>
1 2 3 4 5 6 7 8<br>
<br>
With the 2 conditions in mind, my questions are:<br>
<br>
2. a) Is there some way for me to store the entire file name as the matrix name?<br>
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?

Mon, 02 Jul 2012 16:45:43 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#881677
dpb
On 7/1/2012 5:39 PM, Stan wrote:<br>
> I am importing several data files and plotting data from these files on<br>
...<br>
> I can use fileparts to extract the alphanumeric part of the filename<br>
> (i.e. the part without the extension). This is the part that I must now<br>
> use in the legend. My usage of fileparts:<br>
><br>
> files = dir('*.dat');<br>
> for k = 1:numel(files);<br>
> file=files(k).name;<br>
> [pathstr,name,ext] = fileparts(file);<br>
> d=importdata(file);<br>
> Matrix=d.data;<br>
> ..<br>
> ..<br>
> ..<br>
> end<br>
><br>
> Problem 1:<br>
> 1. ) Is there some way for me to dynamically generate the legend<br>
> headings, from the filename (i.e. from the name variable)?<br>
<br>
Sure...just use<br>
<br>
files(k).name<br>
<br>
as the text argument to legend() if that's what you want or use it or <br>
appropriate substrings from it in conjunction with whatever else you <br>
wish and build another specific string...<br>
<br>
Or, you can do sotoo...<br>
<br>
d = dir('*.dat');<br>
f=d(1).name;<br>
x=importdata(f);<br>
[~,n] = fileparts(f));<br>
l{1}=n;<br>
plot(x(:,1),y(:,2))<br>
hold on<br>
for 2 = 1:numel(files)<br>
f=d(i).name;<br>
x=importdata(f);<br>
[~,n] = fileparts(f));<br>
l{i}=n;<br>
...<br>
plot(x(:,1),y(:,2))<br>
end<br>
legend(l)<br>
<br>
> Problem 2:<br>
> I need to save each extracted data set to a matrix (n rows X 2 columns)<br>
> as I extract it (from each file) and then horzcat() the n matrices (all<br>
> n matrices will have the same size).<br>
...<br>
<br>
If they're all the same length, simply writing<br>
<br>
z=[z x];<br>
<br>
does it. Write the above w/ z as the first case to initialize it then <br>
put the concatenation inside the loop.<br>
<br>
Unless the data are very large the automagic "growing" of the array <br>
won't be terribly noticeable. If it is, then preallocate an estimate <br>
for the number of columns and address directly and check for overflow <br>
and reallocate if needed.<br>
<br>
Could be added as cells in a cell array simply by indexing w/ the {} <br>
curly brackets.<br>
<br>


Wed, 04 Jul 2012 21:21:07 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#881900
Stan
Oh that's a very nice way to generate the matrix. Thanks for that.<br>
<br>
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. <br>
<br>
>> mat2=rand(2048,5);<br>
<br>
>> numel(files)<br>
<br>
ans =<br>
<br>
5<br>
<br>
>> NNN<br>
<br>
NNN = <br>
<br>
'block'<br>
'block2'<br>
'block3'<br>
'block4'<br>
'block5'<br>
<br>
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.<br>
<br>
x=1:(a_setup*b_setup)num_nan;<br>
>> size(x)<br>
<br>
ans =<br>
<br>
1 2048<br>
<br>
x is a 1 X 2048 double. It is just a series of numbers from 1 to 2048. It can be generated as follows:<br>
x = 1:2048; %given the dimensions of mat2, x' will have to be graphed<br>
<br>
So:<br>
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.<br>
<br>
MY ATTEMPT AT THE LOOP FOR THE GRAPH:<br>
for qqqra = 1:numel(files); %from 1 to 5<br>
bbbb(qqqra,1) = plot(x',mat2(:,qqqra),'LineWidth',2);<br>
end<br>
<br>
Explanation of the 2nd line of the loop:<br>
 take x' as the x and qqqra (as the column number from the matrix mat2) as the y and plot them<br>
<br>
QUESTION:<br>
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?

Wed, 04 Jul 2012 22:41:55 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#881902
dpb
On 7/4/2012 4:21 PM, Stan wrote:<br>
...<br>
> ... Here's the setup I am working with.<br>
>>> mat2=rand(2048,5);<br>
><br>
...<br>
<br>
> NNN =<br>
> 'block'<br>
> 'block2'<br>
> 'block3'<br>
> 'block4'<br>
> 'block5'<br>
><br>
> NNN is a 5 X 1 cell. The cell entries are what I must use as legend<br>
> entries on my graph. This, I believe, is the source of my problems.<br>
<br>
Surely don't see what that would be (yet, anyway...)<br>
<br>
...<br>
<br>
> x = 1:2048; %given the dimensions of mat2, x' will have to be graphed<br>
<br>
In that case, plot(Y) will plot the columns of Y vs their index w/o any <br>
X input at all...<br>
<br>
doc plot<br>
<br>
But also note that if you need a column vector, then write<br>
<br>
x=[1:length(mat2)]';<br>
<br>
and get the orientation you want/need from the gitgo.<br>
<br>
> So:<br>
> There is a matrix mat2, with 5 columns. Each column represents a 'y'<br>
> variable that I wish to plot against a common x, all (columns) on the<br>
> same graph.<br>
<br>
So, what's wrong w/<br>
<br>
plot(x,mat2)<br>
legend(NNN)<br>
<br>
???<br>
<br>
At the command line here, even using your variable names...<br>
<br>
>> mat2=rand(2048,5);<br>
>> l='';for i=1:5,l=strvcat(l,['Block ' num2str(i)]);end<br>
>> NNN=cellstr(l);<br>
>> plot(mat2)<br>
>> legend(NNN)<br>
>><br>
<br>
Given the nature of rand() data as random over the same range the <br>
resulting plot is pretty much a solid bunch of squiggles but presuming <br>
you have an actual data set of some other type in mind, it produces a <br>
plot and legend...<br>
<br>
> MY ATTEMPT AT THE LOOP FOR THE GRAPH:<br>
> for qqqra = 1:numel(files);<br>
> bbbb(qqqra,1) = plot(x',mat2(:,qqqra),'LineWidth',2);<br>
> end<br>
><br>
...<br>
<br>
> QUESTION:<br>
> I need the legend to update its entries as graphs are added/removed. How<br>
> do I now incorporate the legend (given that the entries are stored in<br>
> NNN) so that I can dynamically add/remove legend entries to the plot as<br>
> graphs are added/removed?<br>
<br>
Well given the length of the vector, maybe I can guess this is a <br>
measured signal and the data will overlay pretty much and you want to <br>
just watch them go by one at a time...<br>
<br>
If so to do this sequentially,<br>
<br>
for i=1:numel(files)<br>
plot(mat2(:,i))<br>
legend(NNN(i))<br>
pause(0.5)<br>
end<br>
<br>
Does that help?<br>
<br>
BTW, if you want a color change from the default 'b' that plot() will <br>
reset to each time, save the letter mnemonics of choice (as documented <br>
in plot() ) in an array and pass one of them w/ an index in the modifier <br>
triplet position.<br>
<br>


Wed, 04 Jul 2012 23:30:08 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#881904
Stan
> So, what's wrong w/<br>
> <br>
> plot(x,mat2)<br>
> legend(NNN)<br>
> <br>
> ???<br>
> <br>
<br>
This worked.<br>
<br>
> Well given the length of the vector, maybe I can guess this is a <br>
> measured signal and the data will overlay pretty much and you want to <br>
> just watch them go by one at a time...<br>
> <br>
> If so to do this sequentially,<br>
> <br>
> for i=1:numel(files)<br>
> plot(mat2(:,i))<br>
> legend(NNN(i))<br>
> pause(0.5)<br>
> end<br>
> <br>
> Does that help?<br>
<br>
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É

Thu, 05 Jul 2012 03:03:03 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#881911
dpb
On 7/4/2012 6:30 PM, Stan wrote:<br>
...<br>
<br>
>> If so to do this sequentially,<br>
>><br>
>> for i=1:numel(files)<br>
>> plot(mat2(:,i))<br>
>> legend(NNN(i))<br>
>> pause(0.5)<br>
>> end<br>
>><br>
>> Does that help?<br>
><br>
> Thsi is actually what I was trying unsuccessfully. I tried this, without<br>
> that pause(0.5) line. It said ``Ignoring extra legend entries`` and it<br>
> only displayed one legend entry. Why would this approach not workÉ<br>
<br>
The pause() has no bearing other than to slow it down enough you can see <br>
it happen...otherwise you'll not see anything except the last as the <br>
other will just have a flicker and then show the last plot.<br>
<br>
If you got an error you did something other than just replicate the <br>
abovewhat, would have to see the exact code to tell.<br>
<br>


Thu, 05 Jul 2012 03:10:55 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#881912
Steven_Lord
<br>
<br>
"Stan " <studentt_44@hotmail.com> wrote in message <br>
news:jt2c43$s4j$1@newscl01ah.mathworks.com...<br>
> Oh that's a very nice way to generate the matrix. Thanks for that.<br>
><br>
> Regarding the legend, I've made a bit of progress with the graph but am <br>
> still having trouble. I think it has to do with what it is that is holding <br>
> my legend entries. Here's the setup I am working with.<br>
<br>
*snip*<br>
<br>
> QUESTION:<br>
> I need the legend to update its entries as graphs are added/removed. How <br>
> do I now incorporate the legend (given that the entries are stored in NNN) <br>
> so that I can dynamically add/remove legend entries to the plot as graphs <br>
> are added/removed?<br>
<br>
One way to do this is to set the DisplayName property of the line when you <br>
create it.<br>
<br>
<a href="http://www.mathworks.com/help/techdoc/ref/line_props.html#DisplayName">http://www.mathworks.com/help/techdoc/ref/line_props.html#DisplayName</a><br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Thu, 05 Jul 2012 03:13:06 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#881913
Stan
> The pause() has no bearing other than to slow it down enough you can see <br>
> it happen...otherwise you'll not see anything except the last as the <br>
> other will just have a flicker and then show the last plot.<br>
> <br>
> If you got an error you did something other than just replicate the <br>
> abovewhat, would have to see the exact code to tell.<br>
> <br>
> <br>
<br>
Here is what I tried:<br>
<br>
for iii=1:numel(files)<br>
plot(mat2(:,iii))<br>
legend(NNN(iii))<br>
end<br>
<br>
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 overwritten so I tried these 2:<br>
<br>
for iii=1:numel(files)<br>
a(iii)=plot(mat2(:,iii))<br>
legend(a(iii),NNN{iii})<br>
end<br>
<br>
then<br>
<br>
for iii=1:numel(files)<br>
aa(iii,1)=plot(mat2(:,iii))<br>
legend(aa(iii,1),NNN{iii})<br>
end<br>
<br>
but each of them did the same thing  only block5 ( the last entry was displayed in the legend).

Thu, 05 Jul 2012 03:42:37 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#881916
dpb
On 7/4/2012 10:13 PM, Stan wrote:<br>
>> The pause() has no bearing other than to slow it down enough you can<br>
>> see it happen...otherwise you'll not see anything except the last as<br>
>> the other will just have a flicker and then show the last plot.<br>
>><br>
>> If you got an error you did something other than just replicate the<br>
>> abovewhat, would have to see the exact code to tell.<br>
>><br>
>> <br>
><br>
> Here is what I tried:<br>
><br>
> for iii=1:numel(files)<br>
> plot(mat2(:,iii))<br>
> legend(NNN(iii))<br>
> end<br>
><br>
> I got just block5 ( the last entry in NNN) in the legend. I couldn't see<br>
> the other entries. I think the plot is being overwritten<br>
<br>
Well, yes...that should be obvious when it was run w/ the pause() in <br>
there...<br>
<br>
> ... so I tried<br>
> these 2:<br>
><br>
> for iii=1:numel(files)<br>
> a(iii)=plot(mat2(:,iii))<br>
> legend(a(iii),NNN{iii})<br>
> end<br>
<br>
Same thing just using explicit handle...<br>
<br>
><br>
> then<br>
><br>
> for iii=1:numel(files)<br>
> aa(iii,1)=plot(mat2(:,iii))<br>
> legend(aa(iii,1),NNN{iii})<br>
> end<br>
<br>
Again, the same thing except changed to write a superfluous array index.<br>
<br>
> but each of them did the same thing  only block5 ( the last entry was<br>
> displayed in the legend).<br>
<br>
If you don't want to overwrite the first, then you need to use 'hold on' <br>
after the first...<br>
<br>
plot(mat2(:,1))<br>
legend(NNN(1))<br>
hold on<br>
for i=2:numel(files)<br>
plot(mat2(:,i))<br>
legend(NNN(i))<br>
end<br>
<br>
That is, in the end, the same thing as the original<br>
<br>
plot(mat2)<br>
legend(NNN)<br>
<br>
I see Steven has pointed out there's now a DisplayName property of the <br>
line object that legend() uses instead of having to either use legend() <br>
itself or to save the handles to the objects returned by legend() and <br>
manipulate them manually. That's kinda' useful; you might want to look <br>
into that as well.<br>
<br>
Overall, I don't really know what effect it is that you're trying to <br>
create. If you don't put a pause() or have some other mechanism going <br>
on to slow down a loop the net effect of any of the loops is going to be <br>
the same as the plot() on the matrix visually so there's where I'm <br>
having trouble following where you think you're heading...<br>
<br>
<br>
<br>


Thu, 05 Jul 2012 16:46:08 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#881993
Stan
The<br>
<br>
>> hold on<br>
<br>
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 yvariable. 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.<br>
<br>
I have 2 related questions:<br>
1. Regarding NNN<br>
>> NNN =<br>
>> 'block'<br>
>> 'block2'<br>
>> 'block3'<br>
>> 'block4'<br>
>> 'block5'<br>
<br>
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:<br>
>> NNN =<br>
>> 'testing'<br>
>> 'counter'<br>
>> 'block'<br>
>> 'block2'<br>
>> 'block3'<br>
>> 'block4'<br>
>> 'block5'<br>
<br>
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.<br>
<br>
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?

Thu, 05 Jul 2012 17:45:33 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#882004
dpb
On 7/5/2012 11:46 AM, Stan wrote:<br>
> The<br>
><br>
>>> hold on<br>
><br>
> has worked. That is producing the effect that I wanted. Thanks a lot. As<br>
> you said, it is the same as the first approach. I was not aware of the<br>
> ability of plot() to plot an entire matrix as its yvariable. This is<br>
> where my problem was occuring. Also, I didn't know that legend(NNN) was<br>
> sufficient. I thought that individual legend entries had to be extracted<br>
> and printed to the legend one at a time. Your posts, and further reading<br>
> on my part, have cleared that up. It is very helpful to know this since<br>
> I have to do this (automatically updating legend) a lot.<br>
<br>
I _might_ point out<br>
<br>
doc plot % here if I were to be snarky, but I won't... :)<br>
<br>
legend() adds whatever is passed to it as rows in a character string <br>
array or as cellstrings. It automagically correlates the linestyle of <br>
the nth line to the corresponding entry; if you want those to correspond <br>
you have to pass the correct number of strings. You can make other <br>
characteristics by judicious other choices.<br>
<br>
In your case of plotting a column at a time, it's passing the one <br>
corresponding string in conjunction w/ 'hold on' that makes it show up <br>
as it does as all five in the end (the same as if use the array form for <br>
plot and the full cell array at one call).<br>
><br>
> I have 2 related questions:<br>
> 1. Regarding NNN<br>
...<br>
<br>
> NNN is a 5 X 1 cell. I need to add 2 entries to the left (or top) of<br>
> this cell. I need it to be:<br>
>>> NNN =<br>
>>> 'testing'<br>
>>> 'counter'<br>
>>> 'block'<br>
>>> 'block2'<br>
>>> 'block3'<br>
>>> 'block4'<br>
>>> 'block5'<br>
><br>
...<br>
<br>
> I have tried, with no luck, to convert NNN into a 1 column matrix and<br>
> then add rows. Is there another way to accomplish this?<br>
<br>
Well, you haven't shown what you tried and again it looks like you're <br>
not using the doc's very effectively... :(<br>
<br>
Remember from some time back I built NNN (and I really _do_ wish you'd <br>
use meaningful variable naming; it would make things much easier to <br>
remember what is what and surely make your code much more readable and <br>
hence both more easily written as well as debugged. Not to mention <br>
trying to maintain it for some length of time) that 'l' was a character <br>
array we used cellstr() on to get the cellstring array....<br>
<br>
>> cellstr(strvcat('Some new stuff',l)) % using the character array<br>
ans =<br>
'Some new stuff'<br>
'Block 1'<br>
'Block 2'<br>
'Block 3'<br>
'Block 4'<br>
'Block 5'<br>
>> cellstr(strvcat('Some new stuff',char(NNN))) % convert then go back<br>
ans =<br>
'Some new stuff'<br>
'Block 1'<br>
'Block 2'<br>
'Block 3'<br>
'Block 4'<br>
'Block 5'<br>
>> cellstr(strvcat('Some new stuff',NNN))<br>
??? Error using ==> char<br>
Inputs must be character arrays.<br>
<br>
Error in ==> C:\matlabR12\toolbox\matlab\strfun\strvcat.m<br>
On line 23 ==> s = char(varargin{notempty});<br>
<br>
>><br>
<br>
The last is a response to the note I see in current doc that strvcat() <br>
is to be removed in a future version and the functionality has been <br>
placed in char(). Depending on your version I don't know when that <br>
happened but the previous behavior is the latter.<br>
<br>
doc cellstr<br>
doc char % and follow the links to their friends and explore<br>
help strings % overview<br>
help strfun % see the complete menagerie<br>
<br>
It would seem there really should be an 'strinsert' function or some <br>
other way a little less verbose but afaict this is still it...<br>
<br>
And the second question was????<br>
<br>


Fri, 06 Jul 2012 04:06:08 +0000
Re: Data Reading/Plotting: Matrix names as alphanumeric filenames
http://www.mathworks.com/matlabcentral/newsreader/view_thread/321473#882051
Stan
Ah thanks. That was easy. char worked. I simply inserted as indicated and it did the same thing.<br>
<br>
I had previously tried:<br>
horzcat<br>
vertcat<br>
cell2str followed by horzcat<br>
<br>
None of those were working.<br>
<br>
Also, there was no other question in my previous post....just that one regarding NNN.<br>
<br>
Thanks again for all the help. I think that this thread has been resolved.