tags:

views:

102

answers:

1

Hi there,

I have a problem that I thought I knew how I can fix it, but apparently I failed ..

I got a .mat file that I created, it has 2 columns and 25 rows of numbers. I would like to do a loop to get each value in the first column and put it in the X value, and the second column in the Y value. I then need to plot the points on the graph.

I know how to do the loop, and the plotting .. but I failed to extract the data and put them in X and Y values.

This is my trial code :

        load figureinfo.mat
        for K=1:25
            x=X(:,K) ~~ I remember that the code looks something like that to extract ..
            y=Y(:,K)
            plot(x,y,'o')
            hold on
        end

Can you please remind me how to get the data and put it in X and Y ? Thanks !!

EDIT

If someone can also add, where is "ROWS" in (:,b) ?? b=Columns .. but where do I put the rows ?

+3  A: 

Try the following:

load figureinfo.mat; %# assume this contains a matrix called figureinfo
X = figureinfo(:,1); %# numbers from all rows, column 1, into X
Y = figureinfo(:,2); %# numbers from all rows, column 2, into Y
plot(x,y,'o');

Or more simply,

load figureinfo.mat;
plot(figureinfo(:,1), figureinfo(:,2), 'o');

If you don't know the name of the matrix in your .mat file, I recommend:

clear %# clear all variables from workspace
load figureinfo.mat;
whos

which will show the the name, size, and datatype of whatever you just loaded.

If you really want to extract the data in a loop, you have two options:

load figureinfo.mat; %# assume this contains a matrix called figureinfo
X = [];
Y = [];
for ctr = 1:length(figureinfo)
    X = [X figureinfo(ctr,1)];
    Y = [Y figureinfo(ctr,2)];
end

or (faster because it doesn't keep reallocating X and Y)

load figureinfo.mat; %# assume this contains a matrix called figureinfo
X = zeros(length(figureinfo),1);
Y = zeros(length(figureinfo),1);
for ctr = 1:length(figureinfo)
    X(ctr) = figureinfo(ctr,1);
    Y(ctr) = figureinfo(ctr,2);
end
mtrw
Thank you very much .. Im getting `??? Undefined variable figureinfo.` .. Is this because I may have created an incorrect mat file ?? I only have two columns, and they have numbers .. looks valid right ?
ZaZu
I assumed that the variable in your `.mat` file was called `figureinfo`. That's a bad assumption on my part. Try doing `clear; load figureinfo.mat; whos` to find the real name of the matrix.
mtrw
Ahhh that worked, thanks !!!!! :) One more thing, if I want to do a loop to extract each value row by row and plot it, where do I put "K" that in (:,2) ??
ZaZu
I edited to add the loop info. The rule to remember is that the indeces are `row,column`. The `:` operator pulls a whole row/column out for you, so that you don't have to write the loop yourself. In your initial example, `x = X(:,K)` means 'pull the numbers in all rows, K'th column from X and put them in x`.
mtrw
Thanks for the extra info !! really helpful !! I appreciate all this :)
ZaZu
Actually, can you tell me what's in `figureinfo.mat`? I assumed it was one matrix, but in looking at your question again I realize that it actually has two matrices X and Y. Is that right, and what are their sizes?
mtrw
I just got the loop to work. For your question, the first column has X coordinates and the second Column has the Y coordinates .. Regarding the loop, all I had to do is put a `pause(0.5)` and `hold on` .. while having `K` (the function of the loop) instead of `:` then the points plot as needed :)
ZaZu
Cool, glad it's working. You might try `drawnow()` instead of `pause` to make the graph update.
mtrw
Will check it out, thanks :)
ZaZu