Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Tuesday, August 23, 2005

Curve fitting in Matlab

Stolen off of http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=6716&objectType=file

%prepare some data
% xdata=0:0.1:10;
% ydata=2+7*xdata+6*randn(size(xdata));
% %orthogonal linear fit
% p=linortfit(xdata,ydata)
% yy=p(1)+p(2)*xdata;
% %compare with normal linear regression
% p0=polyfit(xdata,ydata,1);
% yy0=polyval(p0,xdata);
% %plot to compare data with linear fits
% plot(xdata,ydata,'.',xdata,yy,xdata,yy0,':');

Wednesday, June 29, 2005

Posting Code to Blogger

Ok, I have griped about this in the past-- it's hard to post code in blogger. Well here's a start to doing it:

PRE tag

You can use the tag pre to keep the formatting of text. If I paste Matlab code into blogger, for example, it removes the tabs and spaces. Using <PRE> prevents that.

Characters interpreted as Code

This is the annoying bit-- you can use <, &gt, " and they won't get interpreted. That's the best I have for now.

Friday, April 01, 2005

Getting Around Matlab's Axes Formatting

Matlab likes to format your axes into scientific notation whenever the numbers are large or small. There doesn't seem to be any quick way to turn this off-- the axes labeling is unaffected by 'format' or anything like that. Luckily, the axis object contains both the raw floating point values of the tick marks and also an array of strings that Matlab automatically creates. What you can do is this:

numericticks=get(gca,'XTick'); % get the values of the ticks
fullticks = num2str(numericticks','%7.0f'); % save as strings
set(gca,'XTickLabel',fullticks); % set the text labels to force
% MATLAB not to use scientific
% notation


This gets the tick values of the X axis of the current axes. It then converts them to strings and updates the string array in the axis object, overwriting the Matlab generated ones. Just set the formatting string in the call to num2str for you needs. Note that the matrix numericticks needs to be transposed in this call otherwise Matlab flips out when you set the tick labels.

Monday, March 28, 2005

Matlab Arrays

Array indices start at 1. Supposedly length arrayname gets the length of the array but I have found that while it produces the correct results at runtime in conditional statements, it does not always return the correct value when run by itself at the prompt or in a script at runtime. Use max(size(arrayname) for this instead, but this is not defined for empty arrays.

Addressing elements of arrays:
0 < face="courier new">arrayname(3:5)

would return i3, i4, and i5.

Numeric: myarray(i)
Numeric arrays store floats or ints.

Cell: myarray{i}
Cell arrays store pointers to other arrays. In the case of varargin, these are often single element arrays.

http://www-ccs.ucsd.edu/matlab/techdoc/apiref/thematlabarray.html

Friday, March 18, 2005

Drawing Text in Matlab Outside of Existing Axes

If you want to draw text with Matlab outside of existing axes, you must create an invisible axes and then place visible text in it. Here is a function to do this.

function thandle = puttext(usertext, userpos, textsize)
% Textsize is as in Word, etc
% userpos is a 1x2 or 1x3 array of values where value x is 0<=x<=1
% set h0 to the handle of the current figure
h0 = gcf;
% create invisible axes to put a text control inside of
h1 = axes('Parent',h0, ...
'CameraUpVector',[0 1 0], ...
'Color','none', ...
'CreateFcn','', ...
'HandleVisibility','off', ...
'HitTest','off', ...
'Position',[0 0 1 1], ...
'Tag','ScribeOverlayAxesActive', ...
'Visible','off', ...
'XColor',[0.8 0.8 0.8], ...
'XLimMode','manual', ...
'XTickMode','manual', ...
'YColor',[0.8 0.8 0.8], ...
'YLimMode','manual', ...
'YTickMode','manual', ...
'ZColor',[0 0 0]);
% create the text control
h2 = text('Parent', h1, ...
'Color',[0 0 0], ...
'FontSize',textsize, ...
'Position',userpos, ...
'String',usertext);
% return the handle of the text control
puttext = h2;
return

File Reading & Graphic Tips for Matlab

Here's how I read in formatted text, ignoring all lines that start with '#'

[cpu0usr cpu0sys cpu0idle cpu1usr cpu1sys cpu1idle] = textread(inputfile,'%u %u %u %u %u %u', -1, 'commentstyle', 'shell');

Here's how I'd graph this stuff (in 3d):


subplot(1,2,1), plot3(time,z0, cpu0usr, '', time, z0+1, cpu0sys, '', time,z0+2, cpu0use, ''), ...
xlabel('time(s)'), zlabel('% utilization'),title('CPU0'), ...
legend('CPU 0 usr (0)', 'CPU 0 sys (1)', 'CPU 0 usage (2)'), ...
grid on, axis([0 max(time) -0.5 +2.5 0 100]), ...
view([-15 15]), axis square, shading interp, zoom(1.7);


Here's how to write it to a file:

I = getframe(gcf);
newfilename = sprintf('%s.png',filetoprocess);
fprintf('Writing to file %s...', newfilename);
imwrite(I.cdata, newfilename);


Here's some useful graphing stuff:

orient landscape portrait - orient the page
clf reset - clear the screen completely... none of this leaving text behind stuff
set(gcf, 'Color', [1 1 1]); - set the background color to white

And finally, putting up text non interactively. This is actually not so easy-- you need to create an axes in order to create text. So if we want to put text outside of the existing axes, we have to make an invisible axes and then put visible text in it. Got it? Here's some code for you...


function thandle = puttext(usertext, userpos, textsize)
% Textsize is same as word
% Userpos is a 1x2 or 1x3 array where each value x is 0<= x <=1
% Usertext is uh text.
% set h0 to the handle of the current figure
h0 = gcf;
% create invisible axes to put a text control inside of
h1 = axes('Parent',h0, ...
'CameraUpVector',[0 1 0], ...
'Color','none', ...
'CreateFcn','', ...
'HandleVisibility','off', ...
'HitTest','off', ...
'Position',[0 0 1 1], ...
'Tag','ScribeOverlayAxesActive', ...
'Visible','off', ...
'XColor',[0.8 0.8 0.8], ...
'XLimMode','manual', ...
'XTickMode','manual', ...
'YColor',[0.8 0.8 0.8], ...
'YLimMode','manual', ...
'YTickMode','manual', ...
'ZColor',[0 0 0]);
% create the text control
h2 = text('Parent', h1, ...
'Color',[0 0 0], ...
'FontSize',textsize, ...
'Position',userpos, ...
'String',usertext);
% return the handle of the text control
puttext = h2;
return

Saturday, March 05, 2005

Loading and Saving Files with Matlab

Load and Save are the simplest high level functions. Other high level functions are available for comma delimited files, etc, as well as C style sprintf like functions.

http://math.carleton.ca/~help/matlab/MathWorks_R13Doc/techdoc/matlab_prog/ch8_pr16.html

Labels

Blog Archive

Contributors