You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Plot Multiple Time Series Based on Individual Condition
6 views (last 30 days)
Show older comments
Hello Friends!
I am somewhat new to scientific charting) more accustomed to working in Excel, but I think I have run into its limitation as regards to charting…I am not sure if Matlab can do the following…
1) Take the time series data from excel, each data series will have three describers (first three rows of each column) – COLOUR, TYPE and THICKNESS 2) plots each time series data in such a way as: a) colors the time series according to a time series criterion (eg. 1 for red, 2 for black) b) allows to adjust the thinness of the plotted series based on another criterion (e.g. 10 very thick, 2- close to hairline) c)Sets the type of the trendline to dashed/solid (based on the Type criterion)
I can send an example worksheet if necessary..
Thanks for your time!) I will be glad to hear any opinion.
Dima
Accepted Answer
Walter Roberson
on 14 Sep 2011
You may wish to create some cell arrays to help:
colors = {'r', 'k', 'g'}; %red, black, green
linetypes = {'--', '-'}; %dashed, solid
% ....
thislinespec = [colors{column1}, linetypes{column2}];
thisthick = column3;
plot(x, y, thislinespec, 'thickness', thisthick);
12 Comments
Dima
on 14 Sep 2011
I wonder if you have some examples of such a code????:) I mean do you think there is a complete code to import excel data and plot it?
thanks a lot again!)
Walter Roberson
on 14 Sep 2011
I do not have that code handy.
Is each row of your data a different time series? Do times and data values alternate, or is there a starting time and then a list of data values with an assumed constant time interval?
Dima
on 14 Sep 2011
thank you very much for the fast reply, please see the following excel file for an example of the time series that I wish to plot (http://ifile.it/3o90rvu) the following is the snapshot of the first few rows:
1 1 0 1 COLOUR
1 2 1 2 TYPE
1 2 6 9 THICKNESS
1 1.040236104 1.038653607 0 0
2 1.039803766 1.038216885 0 0
3 1.039371429 1.037780164 0 0
4 1.038939091 1.037343443 0 0
5 1.038506753 1.036906721 0 0
6 1.038074416 1.03647 0 0
7 1.037642078 1.036033279 1.0624 0
8 1.03720974 1.035596557 1.061273448 0
9 1.036777403 1.035159836 1.060146897 0
Dima
on 14 Sep 2011
so each time series starts at row 4 and all the criterion are placed above each time series....you think this way of placing the line qualities the best way to place them???
Walter Roberson
on 14 Sep 2011
When you have variable amounts of data, it is easier parse if you put the constant part first and then the variable stuff. Thus I would suggest you move the words 'COLOUR' and 'TYPE' and 'THICKNESS' to be the first thing on their lines.
I gather that after that, the first thing on the line is a time, and the remaining items are the values of each of the time series at that time? Is there ever any missing data, and if so how is it indicated?
And to cross-check: you have 4 time series in that file? Or is the trailing 0 on the lines just to match the number of columns in the first 3 lines? If that is the case, is the initial 1 on the first three lines so that that information will sort first? And if so, if the data is being sorted, is the order of the 3 headers? If the initial 1 is to get the headers to sort first, would there not be a risk that some of the data lines might sort before the headers because their times happened to be earlier than "1" ? If you do for some reason need a constant "1" at the beginning of the header lines, then I would suggest moving the keyword to the second position unless there is a technical reason not to.
Dima
on 14 Sep 2011
thanks again for the quick reply) I changed the placing of the criteria for each line, there are 4 lines, each labeled with a number, then in the separate part of the sheet the qualities for each of the four lines are listed in a table with three headings (for each criteria) and the corresponding values for each line....the lines each start at different times....the time is the row number itself.....there are no missing values - only times when the line does not exist (e.g.E8) in the updated file (http://ifile.it/rw9pzh8)
Walter Roberson
on 14 Sep 2011
Consider the process of automating this when you do not know the number of timeseries ahead of time. You would have to ask to fetch the first row of the file, and search within that row looking for the keywords, and deduce the number of time series by looking to see which column the keywords start in. Possible, but probably not ideal.
Automation would be easier if the format were something like
A1 - contains number of time series
B1:B(=A1) - contain colour information
C1:C(=A1) - contain type information
D2:D(=A1) - contain thickness information
(E to end)1 - times
(E to end)(2:1+=A1) - values
Dima
on 14 Sep 2011
yes I am looking exactly into automating this procedure in the future with previously unknown number of the time series to plot...do you think you can help me create a routine for plotting these four lines in the way that you described? maybe even to plot a single first series with three qualifiers listed in the last file I attached? I will happily continue from that...thank you so much for your attention to this question!)
Fangjun Jiang
on 15 Sep 2011
@Wlater, What is 'thickness'? I could not specify that property. I could use 'linewidth'.
Walter Roberson
on 15 Sep 2011
Fangjun, 'thickness' refers to the thickness of my head when I'm working on too many things at one time ;-(
Dima
on 15 Sep 2011
yeah))) thickness must refer to the linewidth)))) Walter hopefully)) your head linewidth will go back to normal)))) Maybe then you can still help me create this kind of code that can build upon later...thanks!
More Answers (2)
Fangjun Jiang
on 14 Sep 2011
Yes, all is pretty straightforward in MATLAB. You can type help plot or doc plot in MATLAB for more information. Try this example for yourself.
figure;hold on
plot(1:10,'r','linewidth',10)
plot(rand(10,1),'g--')
Code below applies to a simple example Excel file.
[Num,Txt,Raw]=xlsread('test.xls');
colors = {'r', 'k', 'g'}; %red, black, green
linetypes = {'--', '-'}; %dashed, solid
figure(1);hold on;
for k=2:4
plot(Num(4:end,1),Num(4:end,k),[colors{Num(1,k)},linetypes{Num(2,k)}],'linewidth',Num(3,k));
end
25 Comments
Dima
on 15 Sep 2011
I wonder if you might have a chance to craft some into code that can handle this task??? I am new to Matlab and woudl appreciate if you could help me out)
Fangjun Jiang
on 15 Sep 2011
I copied the below data to an Excel file called test.xls, note each data should be in a cell so the data will occupy from A1 to E12. Use the code in my answer to plot it.
1 1 2 3 COLOUR
1 2 1 2 TYPE
1 2 6 9 THICKNESS
1 1.040236104 1.038653607 0 0
2 1.039803766 1.038216885 0 0
3 1.039371429 1.037780164 0 0
4 1.038939091 1.037343443 0 0
5 1.038506753 1.036906721 0 0
6 1.038074416 1.03647 0 0
7 1.037642078 1.036033279 1.0624 0
8 1.03720974 1.035596557 1.061273448 0
9 1.036777403 1.035159836 1.060146897 0
Dima
on 15 Sep 2011
that is so great!) thank you so much!) I tried to run the code you supplied above and it does plot one time series on a chart in red color and solid line type, the other time series are not plotted for a reason,
here is the message from Matlab:
[Num,Txt,Raw]=xlsread('test.xls');
colors = {'r', 'k', 'g'}; %red, black, green
linetypes = {'--', '-'}; %dashed, solid
figure(1);hold on;
for k=2:4
plot(Num(4:end,1),Num(4:end,k),[colors{Num(1,k)},linetypes{Num(2,k)}],'linewidth',Num(3,k));
end
Warning: Could not start Excel server for import, 'basic' mode will be used. Refer
to HELP XLSREAD for more information.
> In xlsread at 176
??? Subscript indices must either be real positive integers or logicals.
Do you think you can see if this can be corrected??
Thanks a lot for your time!)
Dima
Dima
on 15 Sep 2011
I have also upload a snapshot of the figure chart at
http://ifile.it/fjhgbqd
Do you also see the same picture?
thanks!)
Fangjun Jiang
on 16 Sep 2011
Do you have MS Excel on your computer? Is your computer a PC or Mac? The error message is about xlsread() function. Type doc xlsread to find out more.
Dima
on 16 Sep 2011
Yes I have excel 2003 on Vista....you mean this error -"Subscript indices must either be real positive integers or logicals." is about not being able to load data properly from excel? or is it due to Matlab not understanding the inputs?
Dima
on 16 Sep 2011
maybe there is something different in the excel sheet that I am using..do you think you can upload your version to ifile.it or any other place?
thanks!
Fangjun Jiang
on 16 Sep 2011
That site requires registration or extra tickets. I won't do it. You can construct the Excel file according to my previous comment. Just put every space-delimited item in a separate cell so you get every cell filled from A1 to E12. Then the code should run without any problem.
Fangjun Jiang
on 16 Sep 2011
Regarding the Excel error, refer to this post but I don't expect your Excel 2003 on Vista has that problem.
http://www.mathworks.com/matlabcentral/answers/14817-reading-excel-files-on-a-mac
Dima
on 16 Sep 2011
thank you for your reply...I constructed the workbook exactly like you specified in the post above and it does not plot all the time series....can you please show me the kind of the chat you are getting???
That is so much appreciated....you can send it to my email..vincemaddox1024 at hotmail.com
Dima
on 16 Sep 2011
wowoowwoowowoowowooowowowowowo it works!!! thanks so much for sending the file to my email...I was wondering if it can be possibel to make the dashed line with the smaller dots instead of the dashes???? and do you think it can be possibel to make the x axis the time axis instead of the single counter???? thank you again of your enormous contribution!!!
Fangjun Jiang
on 16 Sep 2011
I think you've opened the door. You can type in help plot or doc plot in MATLAB Command Window to see how to change the color, marker and line style. In terms of x axis, plot(x,y) will use whatever data you have in the variable x.
Dima
on 16 Sep 2011
great thanks! I will try to change the line type and x data to have the time there and will let you know) thanks again!)
Dima
on 16 Sep 2011
I was able to change the lien type to dotted)) I added a new columnt similar to the original three and it says:
??? Index exceeds matrix dimensions.
please let me know how it is possible to plot more time series...
I changed the following code:
for k=2:4
to
for k=2:5
and it does not seem to work(
Thanks!)
Fangjun Jiang
on 16 Sep 2011
The "for k=2:4" statement means to plot the data from 2nd to 4th columnn, if you change it to "2:5", you need to have data in the 5th column.
Go through the "Gettting Started" portion of the MATLAB document, you'll learn lots of the basics.
Dima
on 16 Sep 2011
thanks...yes I did add the data in the 5th column and ran your code with the k=2:5 and it gives off the above error...(??? Index exceeds matrix dimensions. ) I surely need to read much more)) but I woudl be so thankful if you could show me how to add mroe columns to this calculations...that is the workbook I am using now:
1 1 2 1 1 COLOUR
1 2 1 2 3 TYPE
1 1 2 1 5 THICKNESS
1 5.678629242 7.703233636 4.445358125 5.954986076 0
2 5.660599394 7.685203788 4.40766875 5.955991899 0
3 5.642569545 7.667173939 4.369979375 5.956997722 0
4 5.624539697 7.649144091 4.33229 5.958003544 0
5 5.606509848 7.631114242 4.294600625 5.959009367 0
6 5.58848 7.613084394 4.25691125 5.96001519 0
7 5.570450152 7.595054545 4.219221875 5.961021013 0
8 5.552420303 7.577024697 4.1815325 5.962026835 0
9 5.534390455 7.558994848 4.143843125 5.963032658 0
10 5.516360606 7.540965 4.10615375 5.964038481
11 5.498330758 7.522935152 4.068464375 5.965044304
12 5.480300909 7.504905303 4.030775 5.966050127
13 5.462271061 7.486875455 3.993085625 5.967055949
14 5.444241212 7.468845606 3.95539625 5.968061772
15 5.426211364 7.450815758 3.917706875 5.969067595
16 5.408181515 7.432785909 3.8800175 5.970073418
Fangjun Jiang
on 16 Sep 2011
I added a column and didn't have any error. You need to learn how to debug your code. Save the code in a script. Put a breakpoint and run the script one line at a time to see where is the problem.
Dima
on 17 Sep 2011
thanks)) I have two days worth of experience of using matlab so I might need just a little bit more assistance from you)) I used the following excel file (http://www.sendspace.com/file/glvr6l) and this code:
%%
[Num,Txt,Raw]=xlsread('test.xls');
colors = {'r', 'k', 'g'}; %red, black, green
linetypes = {'--', '-'}; %dashed, solid
figure(1);hold on;
for k=2:5
plot(Num(4:end,1),Num(4:end,k),[colors{Num(1,k)},linetypes{Num(2,k)}],'linewidth',Num(3,k));
end
and I still get the error - not sure why....I understand the k runs to 5 and plots each column from 2 to 5 so I am not sure why it would give an error like that...thanks...
Fangjun Jiang
on 17 Sep 2011
In the code, linetypes = {'--', '-'}, means two options. In your Excel file, you have line style valued at 3, that's the problem. Did you mix color and line style?
Dima
on 17 Sep 2011
that is just superb!!!) thank you for your help...I AM STARTING TO FEEL THE real power of this programm...I wonder if it can be possible to add a text label at the start of each line from the heading(one extra row above the describers we have already)? I would happily code it myself))) right now - actually desire it so much..)))..Short update on my education progress -I got the book on Matlab today (Matlab:A Practical Introduction to Programming and Problem Solving) reading about operators now)))I just cannot have enough of its powerful functionality!)
Fangjun Jiang
on 17 Sep 2011
Glad you like MATLAB programming. Reading that book is a really good start. If you have specific questions, this forum offers lots of help.
Dima
on 17 Sep 2011
yes)) I have one short very specific question if you think the plot function can be used to add a text label to the start of each line??:)good night)
Dima
on 18 Sep 2011
thanks....it is inserted in the list of the arguments of the plot function right?
Dima
on 28 Sep 2011
Fangjun Jiang! Maybe you can also help me with one more questions about the code you provided before....I wonder if it is possibel to specify the colors not in the default format (e.g. 'r') but in the RGB format ( [1 0 0])? I ask this because I need different shades few red and blue...) Thanks!) D
1 Comment
Walter Roberson
on 28 Sep 2011
[Num,Txt,Raw]=xlsread('test.xls');
colors = {'r', 'k', [0.6 0.8 3.9]}; %red, black, green
linetypes = {'--', '-'}; %dashed, solid
figure(1);hold on;
for k=2:4
plot(Num(4:end,1),Num(4:end,k),linetypes{Num(2,k)},'linewidth',Num(3,k), 'color', colors{k} );
end
See Also
Categories
Find more on MATLAB Functions in Microsoft Excel in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)