Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Wednesday, April 15, 2015

plot.ly and matlab

There's some crazy stuff on the internet these days.

I wanted to make an interactive plot for one of my figures that is hard to fully grasp. I remembered the plot.ly software and of course they have added a lot of new stuff since I've last looked. Such as converting matlab figures to plotly.

https://plot.ly/matlab/getting-started/

Which resulted in these abominations.

https://plot.ly/~hsu.leslie/8  (default styling)

https://plot.ly/~hsu.leslie/10  (supposedly my original styling)

Looks like it could be really cool if used properly. Still some conversion issues obviously.

Hope someone I now figures it out and shows me what to do.

Sunday, October 26, 2014

Writing text output of a table

    % open file, write out header
    % write out transpose of table4, with six total places per column, two decimal places (or none for npts)

    fileID = fopen('table4.txt','w');
    fprintf(fileID,'%6s %6s %6s %6s %6s %6s \n',
            'a', 'b1', 'b2', 'R^2', 
            'rmse', 'npts');
    fprintf(fileID,'%6.2f %6.2f %6.2f %6.2f 
            %6.2f %6.0f \n',table4');
    fclose(fileID);

Monday, May 19, 2014

mac vs. pc matlab continued

Might have more posts here soon since I just agreed to be a guinea pig for a software documentation project and they already picked apart my first very simple script for not being componentized, not cross platform, and has lots of runtime dependencies.

From stack overflow. Actual functions to make code cross-platform.

Of course, if you try to access a Windows-style path on a Mac, it will error.
MATLAB includes a set of functions that make it fairly easy to make your code cross-platform with respect to these sorts of issues. Take a look at, for example, the functions fullfilefilepartsfileseppathsepispc, and ismac.

Thursday, January 5, 2012

finding and indexing in matlab

This is something as ridiculous as in 2001 when I didn't know how to use .ppt so I just made .html presentations.

Well I don't know procedural SQL so I'm using matlab to find and index things.

The problem:
I had a big table in a database and you want to fill in a certain column with an index that depends on other relationships in that table.  (Basically, filling in parent-child relationships but each row has several identifiers: name, unique ID, other unique ID.)

The first loop demonstrates how to use "find" with strings: find the index in the array "name" where the cell value (string) equals input_name(i).

for i = 1:length(input_name)
    igsn_output(i) = igsn(find(ismember(name, input_name(i))==1))
end
igsn_output = transpose(igsn_output);

This second one is just weird, it was giving string/cell/ etc. errors but this worked (input_parent was a number but in a cell array).

for i = 1:length(input_name)
    parent_id_output{i} = cell2mat(sample_id(find(ismember(igsn, input_parent(i))==1)))
end
parent_id_output = transpose(parent_id_output);

I pasted it all into excel and then used "concatenate" to form the SQL statements.

This all could've been done in the SQL environment if I knew procedural postgreSQL.

They shouldn't let scientists code.

Tuesday, November 8, 2011

color-code your 2D matrix

a.k.a. look at a 3D bar graph color-coded by height in plan view

Say you have a reasonably-sized matrix and you want to look for patterns in it. Let's define reasonably-sized as something on the order of 15 by 15, you care about what each value is, but it would be tedious to scan the matrix of numeric values.

Say your matrix is Z, maybe it is a 14x24 matrix of different exponents from polynomial fits to different variables using different thresholds or statistical parameters.

surface(Z) won't do exactly what you want because the values in the matrix will be the vertices of the surface, you want each grid box to be color-coded with the matrix values.

One way to do this is to make a 3D bar plot, color the bars by their height (matrix value), and then look at it in plan view. It's sort of cool.


figure;
h = bar3(Z);


% http://www.mathworks.com/help/techdoc/creating_plots/f10-19972.html
% Tell handle graphics to use interpolated rather than flat shading
shading interp
% For each barseries, map its CData to its ZData
for i = 1:length(h)
    zdata = get(h(i),'ZData');
    set(h(i),'CData',zdata)
    % Add back edge color removed by interpolating shading
    set(h,'EdgeColor','k') 
end
colormap('default')
colorbar;
grid off;
view([0,90])


% you can manipulate the axes (xmin xmax ymin ymax zmin zmax colorbarmin colorbarmax)
axis([0 25 0 15 0 1 0.8 1])


Here's the original document I got the code from:
http://www.mathworks.com/help/techdoc/creating_plots/f10-19972.html

Here's an example of the output.
The rows, 1-14 are 14 different polynomial fits.
The columns, 1-24 are 24 different ways of calculating the x variable in the fit (different thresholds or filters).
The value is the Rsquare value, colorbar color codes it from 0.8 to 1.0.

The matrix shows that the 1st and 23rd-25th columns have the worst fits for all 14 relationships. Also, rows 4, 5, 11, 12, 14 do not produce well-behaved polynomial fits.

Really I just think it's a nice quilt pattern.

As an added plus you can use the rotate button to look at it from any angle you want.


Tuesday, October 4, 2011

startup.m (2)

A long time ago I wrote about setting defaults in startup.m.

Today I finally got to running my new MATLAB on my new computer and have been doing things like setting the path and specifying startup.m.

It is probably safer not to rely too much on startup.m, because if you write a program and then send it to someone else, who hasn't set default font and linewidth and etc. like your own startup.m, then things can get ugly.

I set two things in my startup.m (which is placed into a folder near the top of my path)

(1) MATLAB will open up to a specific folder:
cd C:\Users\me\Documents\MATLAB

(2) log everything I type into the commandline into a date-specific text file

diaryname = ['diary\' datestr(now, 'yyyy-mm-dd') '-diary.txt'];
diary(diaryname);


I'm pretty pleased about these two things. 



Friday, March 18, 2011

setting up figure templates

Make your life easier by setting up figure templates for MATLAB

http://gears-tt.blogspot.com/2011/03/templates-why-matlab-is-awesomeness.html

As posted by MATLAB stream on fb.

Friday, December 10, 2010

matlab demo videos

I watched 14 of the demo videos yesterday:

http://www.mathworks.com/products/matlab/demos.html

It took me about 5 years to haphazardly learn most of the things that you could learn in an hour or so watching all of those videos.

Watch the videos.

You learn things like, did you know you can specify something like
mymatrix(10:end);

I've always done
mymatrix(10:length(mymatrix));

Also good:

http://www.mathworks.com/videos/matlab/optimizing-performance.html

I love matlab.

Monday, November 29, 2010

How to put Excel data into Matlab and retain the 'cell' structure

1. Make your cell structure by using curly brackets:
A={1}
2. Copy the excel data to the clipboard
3. Then open up "A" and right click on the top left cell-- choose 'paste excel data'
4. Save your variable A
5. Use it in Matlab:
B = cell2mat(A(row # to start on : row # to end on , column # );

Wednesday, October 13, 2010

signal processing

spectrogram

h = figure;
Nwin=2048;
Twin=Nwin*dt;

[dum,f,t,p] = spectrogram(y,Nwin,0,[],1/dt,'yaxis');

surf(t,f,10*log10(abs(p)),'EdgeColor','none');

periodogram

making an envelope

envelope

downsample

upsample

diff

Friday, October 1, 2010

mac vs. pc, matlab version

There are a bunch of reasons why a matlab script written for a pc might not work on mac and vice versa. Here are some examples (some (most) content contributed from loopy):

1. dir function to display directory listing (like ls in unix)

On a pc, you'll get this:

>> dir

.          ..         pathdef.m


. stands for "this directory" and .. stands for "directory above"

As far as I know, mac does not do this, but just starts with the regular file names. A lot of times I want to cycle though files in a directory, so I'll create an array with all of the file names, populating it with dir. On my pc, I start at n=3 to skip . and ..    but on a mac it should start at n=1. Or I should write some better code that just skips . and ..

2. for some reason my mac version never runs out of memory, whereas on the pc some of my scripts always run out of memory. same computer hardware, but the software handles memory differently.

3. folder / \ are different, and sometimes it cares, sometimes it doesn't.

4. mac can't write to excel files - there might be some individually written codes out there to handle this. [note: ugh who would write to .xls?]

5. writing pdfs in pc usually makes them sideways, or 90 degrees off, the mac version.

If there are any others out there please post.

Thursday, August 26, 2010

matlab + geospatial

futzing around on matlab help site and ran into this:

Geospatial Data Import and Access
http://www.mathworks.com/access/helpdesk/help/toolbox/map/ref/f3-12193.html#f3-32594

which includes things like:

usgs24kdem
Read USGS 7.5-minute (30-m or 10-m) Digital Elevation Models

Really?? I guess you need Mapping Toolbox and I'm not sure if that comes standard.