MATLAB - How to import and plot data from a .mat file to x and y variables?
I have a problem that I thought I knew how I can fix, but apparently I failed ..
I got a .mat
file that I created. It has two 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:
开发者_Go百科 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
How do I get the data and put it in X and Y?
In addition, where is "ROWS" in (:,b)
? b=Columns
, but where do I put the rows?
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
精彩评论