Sometimes it is easier to use a loop to plot something rather than manually typing plot many times. The problem is that all of the lines will have the same color and all the symbols will be the same. This code easily applies color to plots in a loop.
[Matlab] User Interface Functions
One easy method for creating a user interface is a question dialog. This can easily be used with the switch function to run different sections of code.
A more complex but useful process is this:
function UserInterfaceExample
other functions:
SelectFunction = questdlg('What do you want to do?', ...
'Select Function', ...
'Get data from images', 'Get Amplitude information from data','Plot displacement vs. time','Get data from images');
switch SelectFunction
%% Input data from images
case 'Get data from images'
'Select Function', ...
'Get data from images', 'Get Amplitude information from data','Plot displacement vs. time','Get data from images');
switch SelectFunction
%% Input data from images
case 'Get data from images'
'Stuff'
case 'Get Amplitude information from data'
'more stuff'
A more complex but useful process is this:
function UserInterfaceExample
% User Interface Example
% Creates a box with buttons which execute functions
clear;clc;close all;
height = 250;
width = 200;
figure
set(gcf,'Position',[1000 height+200 width height],'Menubar','none')
uicontrol('Style', 'pushbutton', 'String', 'Say Hi',...
'Position', [50 height-50 100 20],...
'Callback', @Function1);
uicontrol('Style', 'pushbutton', 'String', 'Say Bye',...
'Position', [50 height-100 100 20],...
'Callback', @Function2);
uicontrol('Style', 'pushbutton', 'String', 'Draw a Line',...
'Position', [50 height-150 100 20],...
'Callback', @Function3);
uicontrol('Style', 'pushbutton', 'String', 'Draw Shapes',...
'Position', [50 height-200 100 20],...
'Callback', @Function4);
end
function Function1(src,evt)
clear;clc;close all;
fprintf('HI')
end
function Function2(src,evt)
clear;clc;close all;
fprintf('Bye!')
end
function Function3(src,evt)
clear;clc;close all;
figure
line([2 5],[2 5])
end
function Function4(src,evt)
clear; clc; close all;
figure
rectangle('Position',[200 200 300 500],'Curvature', [0 0])
rectangle('Position',[10 10 300 500],'curvature',[1,1])
axis square
end
other functions:
uicontrol
uigetdir
uigetfile
inputdlg
inputdlg
errordlg
helpdlg
listdlg
msgbox
questdlg
uiresume
uiwait
warndlg
endhelpdlg
listdlg
msgbox
questdlg
uiresume
uiwait
warndlg
Posted on Saturday, February 4, 2012
[Matlab] Adding ASCII Charecters for abvanced ploting
Some times in Matlab one may want to add some charecter in the code. Here is an example:
Lets say you want to plot several variables with similar names (like A1, A2, A3) using a for loop as such:
This will work but all of your plots will have the same line type, symbol color etc. Now it you want to add different colors to a plot you can use strings to define the colors. But using the eval function creates problems because you must create a string within a string. usually you can use this notation:
String = 'Here is another "string" in this string'
However with the eval function this does not work for me. However, ASCII charecters can be created using the char command. So to add colors to the plot:
A list of ASCII character codes are found here:
http://www.asciitable.com/
use the char() command to insert the charecters
Here is a good list of colors and symbols to use with these plots:
Lets say you want to plot several variables with similar names (like A1, A2, A3) using a for loop as such:
A1 = 0:5;
A2 = 5:10;
A3 = 10:15;for i = 1:3eval(['plot(A',num2str(i),')'])hold onend
This will work but all of your plots will have the same line type, symbol color etc. Now it you want to add different colors to a plot you can use strings to define the colors. But using the eval function creates problems because you must create a string within a string. usually you can use this notation:
String = 'Here is another "string" in this string'
However with the eval function this does not work for me. However, ASCII charecters can be created using the char command. So to add colors to the plot:
A1 = 0:5;
A2 = 5:10;
A3 = 10:15;
colors = [1 0 0; 0 1 0; 0 0 1];
for i = 1:3
eval(['plot(A',num2str(i),',',char(039),'color',char(039),',colors(i,:))'])
hold on
end
A list of ASCII character codes are found here:
http://www.asciitable.com/
use the char() command to insert the charecters
Here is a good list of colors and symbols to use with these plots:
symbol = {'o','s','^','v','<','>','d','p'};
colors = [ 0 0 0
0.8 0 0
1 0 0
1 0.6 0
0 0.6 0
0 0 1
0 0 0.6
0.8 0 0.8
];
Posted on Friday, January 6, 2012
[Matlab] Training from Steven
here is the link - http://goo.gl/Lu7Qg
Posted on Monday, December 5, 2011
Styles for BibTeX4word - Custom and modified .bst files using windows
I am using Microsoft Windows 7 (and Windows XP at home) and I use Word to create documents. You must first be familiar with Word, Jabref, MikTex, and Bibtex4word before this will make sense. Start by reading the Bibtex4word documentation. These three programs will allow you to automatically create bibliographies in word.
Here is a manual for Miktex
Below is a list of style types that I use regularly:
The first thing to know about this is that it is really anoying. I have been trying to figure this out for weeks and still have had little luck. There are many many websites that give information, but must of the time it is not that helpful.
Finding my .bst files: This is much more difficult that it seems. I though they would be in the "Program Files/miketex2.8" folder. Nope. I finally found them in:
"C:\Users\UserName\AppData\Roaming\MiKTeX\2.8\bibtex" ~ in Windows 7
"C:\Documents and Settings\UserName\Application Data\MiKTeX\2.8\bibtex\bst" ~ in Windows XP
thanks to some hits from this link.
Here is a manual for Miktex
Below is a list of style types that I use regularly:
- ieeetr/cn^s - good for most conferences
- phaip/cn[]^s - actual APL (AIP) file but use et al. for over 5 name which APL does not allow.
- phjcp/cn[]^s - like APL and does not use et al. but has lower caps in names. ~ this can be made to match APL almost perfectly if you highlight all of the references, press ctrl+d, uncheck the small caps option.
- phnflet - like APL without et al. but uses all caps and DOI in wrong place.
- rsc/cn[]^s - RSC reference type such as Lab on a Chip
- achemso/cn[]^s - America chemical society
- http://www.ee.ic.ac.uk/hp/staff/dmb/perl/index.html
- http://web.reed.edu/cis/Help/LaTeX/bibtexstyles.html
The first thing to know about this is that it is really anoying. I have been trying to figure this out for weeks and still have had little luck. There are many many websites that give information, but must of the time it is not that helpful.
Finding my .bst files: This is much more difficult that it seems. I though they would be in the "Program Files/miketex2.8" folder. Nope. I finally found them in:
"C:\Users\UserName\AppData\Roaming\MiKTeX\2.8\bibtex" ~ in Windows 7
"C:\Documents and Settings\UserName\Application Data\MiKTeX\2.8\bibtex\bst" ~ in Windows XP
thanks to some hits from this link.
Posted on Tuesday, November 29, 2011
[Matlab] Loading Large Data Sets
The Old Way to Load DataSets
Lots of my time consists of analyzing huge sets of files recorded by and oscilloscope or some other device. Typically, I would try to use the same name for each file with a number at the end that increase each time. Then I can write a loop to load all of the files, then do analysis and create plots. This causes its own problems for several reasons such as: several loops must be used for different regions of the data (tek0000n for n = 1-9; tek000nn for nn = 10-99; tek00nnn for nnn = 100-999; etc) and broken loops when a single file in the data set is a different length or mission, and many other issues. Such a sustem looks like this where many conditional loops are used to fix the problem:
Read Directory and Load
Recently, google helped me discover a better method where you can actually tell matlab to read and record the files names in a specific folder. The, so long as all the data is the same length, a single loop can be used to import all of the data in a single folder. Here is an example:
The dir command reads the folder \Data, and a structure cell is created with the information for each file. Since I only care about the name, that cell can be extracted and trimmed. Then a single loop can be used to load all of the data into a useful format. To prevent errors if the files are not the same length, each data set is save in its own .mat file. These files can be quickly loaded in Matlab for later use.
Loading several .mat files
Once all of the data is stored in separate .mat files, this data can be easily accessed and plotted. It one were to plot the first 5 data sets that were loaded above, this code would perform the operation:
I realize that there are many non standard matlab functions here, but I encourage people to learn these functions. They have been very useful for me in performing some more advanced actions.
Lots of my time consists of analyzing huge sets of files recorded by and oscilloscope or some other device. Typically, I would try to use the same name for each file with a number at the end that increase each time. Then I can write a loop to load all of the files, then do analysis and create plots. This causes its own problems for several reasons such as: several loops must be used for different regions of the data (tek0000n for n = 1-9; tek000nn for nn = 10-99; tek00nnn for nnn = 100-999; etc) and broken loops when a single file in the data set is a different length or mission, and many other issues. Such a sustem looks like this where many conditional loops are used to fix the problem:
if m <10 & n < 10for i = 1:nTemp = dlmread([File,'/',File,'-0000',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);endend
if m <10 & n < 100 & n > 10for i = 1:9Temp = dlmread([File,'/',File,'-0000',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);endfor i = 10:nTemp = dlmread([File,'/',File,'-000',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);endendif m <10 & n < 1000 & n > 100for i = 1:9Temp = dlmread([File,'/',File,'-0000',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);endfor i = 10:99Temp = dlmread([File,'/',File,'-000',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);endfor i = 100:nTemp = dlmread([File,'/',File,'-00',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);endendif m > 10 & n < 1000 & n > 100for i = m:99Temp = dlmread([File,'/',File,'-000',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);endfor i = 100:nTemp = dlmread([File,'/',File,'-00',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);endendif m < 10 & n > 1000for i = 1:9Temp = dlmread([File,'/',File,'-0000',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);endfor i = 10:99Temp = dlmread([File,'/',File,'-000',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);endfor i = 100:999Temp = dlmread([File,'/',File,'-00',num2str(i),'.txt'],'\t',[17 0 3664 1]);Frequency(:,i) = Temp(:,1);Absorption(:,i) = Temp(:,2);end
end
Read Directory and Load
Recently, google helped me discover a better method where you can actually tell matlab to read and record the files names in a specific folder. The, so long as all the data is the same length, a single loop can be used to import all of the data in a single folder. Here is an example:
clear;clc;close all
cd ..
Temp = dir('Data');
Temp2 = {Temp.name};
FileName = Temp2(3:end);
for i = 1:length(FileName)
Temp = dlmread(['Data/',char(FileName(i))],',',[14 0 10013 2]);
time = Temp(:,1);
ch1 = Temp(:,2);
ch2 = Temp(:,3);
save(['[Matlab]/MData/DataSet',num2str(i),'.mat'],'time','ch1','ch2')
end
return
The dir command reads the folder \Data, and a structure cell is created with the information for each file. Since I only care about the name, that cell can be extracted and trimmed. Then a single loop can be used to load all of the data into a useful format. To prevent errors if the files are not the same length, each data set is save in its own .mat file. These files can be quickly loaded in Matlab for later use.
Loading several .mat files
Once all of the data is stored in separate .mat files, this data can be easily accessed and plotted. It one were to plot the first 5 data sets that were loaded above, this code would perform the operation:
for i = [1:5]
Str = ['MData\DataSet',num2str(i),'.mat'];
load(Str)
eval(['time_',num2str(i),' = time;'])
eval(['ch1_',num2str(i),' = ch1;'])
eval(['ch2_',num2str(i),' = ch2;'])end
figurehold on
for i = 1:5
eval(['plot(time_',num2str(i),',ch1_',num2str(i),')'])
end
hold off
I realize that there are many non standard matlab functions here, but I encourage people to learn these functions. They have been very useful for me in performing some more advanced actions.
Posted on Tuesday, November 22, 2011
HTML / Javascript equation solver
I always wanted to do simple calculations in HTML and create simple online equation solvers. I spent some time on it and have created a simple equation solver. I have include the code here with lots of comments to try and help others who might want to learn. The biggest issue I had was with the button. I had to use a form input of type button rather than a button element (<button></button>). the button element would run the code but then reset everything to the defaults immediately. I still do not understand why. If you want to try this code, copy and past it into notepad and save as a .html file. Then open the file in an internet browser. Questions and comments are welcome.
Here is the equation solver:
Here is the source code:
-------------------------------------------------------
----------------------------------------------------------
Here is the equation solver:
Here is the source code:
-------------------------------------------------------
<! comments look like this. They are just here to help the programmer>
<! This program has embedded java scripts to solve an equation with certain inputs>
<form name="F1"> <! gives the form a name so it can be referenced by the java script>
a: <! text in front of input a>
<! adds a space after a:>
<! adds another space>
<input
type = "text"
name = "a"
value = "12"
size = "4"
/> <! creates input a>
<! makes input a a text box>
<! names input a a>
<! sets the initial value of input a>
<! sets the size of input a>
<! ends the seting for input a>
<br /> <! move to next line>
b:
<input
type = "text"
name = "b"
value = "100"
size = "4"
/>
<br />
<br />
<b> Solution to a + b: </b>
<br />
<br />
Output:
<input
type = "text"
name = "out"
value = ""
size = "10"
/>
<br />
<br />
<input
type = "button"
value = "Calculate"
onClick = "clickButton1()"
> <! Creates a button and assigns its properties>
</form> <! end of form-F1>
<script
type ="text/javascript"> // starts the imbedded javascript
// comments look like this in java script
function clickButton1()
{
var a = document.F1.a.value; //sets variable a = the contents of input/textbox a
a = parseFloat(a); // convers a from a string to a floating number
var b = document.F1.b.value;
b = parseFloat(b);
var c = a + b;
document.F1.out.value = c;
}
</script> <! ends the script section of the file>
</html>
<! This program has embedded java scripts to solve an equation with certain inputs>
<form name="F1"> <! gives the form a name so it can be referenced by the java script>
a: <! text in front of input a>
<! adds a space after a:>
<! adds another space>
<input
type = "text"
name = "a"
value = "12"
size = "4"
/> <! creates input a>
<! makes input a a text box>
<! names input a a>
<! sets the initial value of input a>
<! sets the size of input a>
<! ends the seting for input a>
<br /> <! move to next line>
b:
<input
type = "text"
name = "b"
value = "100"
size = "4"
/>
<br />
<br />
<b> Solution to a + b: </b>
<br />
<br />
Output:
<input
type = "text"
name = "out"
value = ""
size = "10"
/>
<br />
<br />
<input
type = "button"
value = "Calculate"
onClick = "clickButton1()"
> <! Creates a button and assigns its properties>
</form> <! end of form-F1>
<script
type ="text/javascript"> // starts the imbedded javascript
// comments look like this in java script
function clickButton1()
{
var a = document.F1.a.value; //sets variable a = the contents of input/textbox a
a = parseFloat(a); // convers a from a string to a floating number
var b = document.F1.b.value;
b = parseFloat(b);
var c = a + b;
document.F1.out.value = c;
}
</script> <! ends the script section of the file>
</html>
----------------------------------------------------------
Posted on Sunday, August 28, 2011
Subscribe to:
Posts (Atom)