MatLab graph figure from file - matlab

I don't know how to plot a figure of a graph from a file in MatLab.
I've tryed this:
A=readcell("graph4.txt");
G=graph(A(:,1),A(:,2));
plot(G)
-------------------------------
Sample of .txt
ache 4
é-te 2
unhe 4
ilhe 3
olhe 3
inox 0
...

It looks like you want a bar chart here since your data appear to be categorical counts.
You can use bar(X,Y) (MathWorks link), but you'll want to convert the X to categorical first.
Here is an example with your text file. I used readtable() because I like working with tables, but you can use textscan(), fileread(), or whatever you prefer. Also, I like to set the y-axis ticks to be integers for bar plots since a whole number is expected.
fid = "graph4.txt"
data = readtable(fid)
bar(categorical(data.Var1), data.Var2)
set(gca,'ytick',0:10) % set y ticks to integers

Related

How to assign symbols to the data in Matlab

I am trying to plot some data with categories. However, I couldn't manage to plot my data according to the categories and show a legend for them. Therefore, I am asking a little bit help to this issue.
Example data;
Name
Brand
Mt
Enes
Renault
2.6
Avni
Tofaş
2.38
Asaf
Tofaş
3.06
My experience, I have managed to plot these data with two plot command overlaying each other grouping them by Brand. However, this time, one group of data has 2 line (as Tofaş has two data) and the other has only one line data (as in Renault). Thus, x-axis is confusing and not giving a healty graph.
The other issue with this, I can't label the x-axis according to Name when I plot two graph overlaying.
figure
plot(table.Mt, 'o');
xtickangle(90)
sizes = size(table.Name);
a1 = gca;
a1.XTick = [1:sizes];
a1.XTickLabel = table.Name;
the output of the code above
[Ism, Ind] =ismember(table.Brand, 'Tofaş');
plot(Ism, 'o')
the output of second code block above
As you can see, when I select only spesific Brand. The rest of arrray filling with zero (0) which I don't want to.
What I want is that plotting all data with spesific symbols for each Brand together.
Thank you
Enes

Matlab-reading large netcdf files

I have a 17G netcdf file that I am trying to use for analysis. Each variable in the netcdf file is set up like: variable(x,y,z,time). I would like to read in and analyze the variables one 'time' at a time for analysis in Matlab. In other words, I want to use all x, y, and z points at one time. In the past I have had smaller files so reading in a variable has been set up like
fid=netcdf.open('filename/location','NC_NOWRITE');
var_id=netcdf.inqVarID(fid,'varname');
var=netcdf.getVar(fid,var_id);
Is it possible to read in the variables using one time step when the variable is read in? (Incorrect syntax) It'd essentially look like
var=netcdf.getVar(fid,var_id,[:,:,:,time_index]);
Yes, the matlab netcdf command supports this, almost the way you wrote it:
data = netcdf.getVar(fid,var_id,var_index,var_length)
See the matlab documentation for more information. You can also use high-level matlab commands instead of the netCDF library functions.
For example, if varname is a 100x4 array, you could get row 7 by using:
% read 4 columns from 1 row of data starting at row 7, column 1
v = ncread('filename/location','varname',[7 1],[1 4]);
or a four-dimensional array, as in the question:
% read all data from dim. 1-3 at dim 4 = 27
v = ncread('filename/location','varname',[1 1 1 27],[Inf Inf Inf 1]);

Exporting data in zoomed in region of MATLAB plot to workspace

I have 4-5 subplots of timeseries data and scatter plots in a MATLAB figure whose x axes are linked. The data is quite long and I've zoomed into a small portion of the figure.Now I want to export just the data contained in this zoomed in portion to work-space as variables. Is it possible?
For example, the following is the plot for the full dataset.
The following is the zoomed in portion,
Now I want to export all the variables or time-section of the variables corresponding to the above zoomed in portion to workspace.
Building off of Ratbert's comment, let's set up a sample plot to play around with.
x = 1:10;
h.myfig = figure();
h.myaxes = axes('Parent', h.myfig);
h.myplot = plot(x);
I'm going to assume you have MATLAB R2014b or newer, which is where MATLAB switched graphics handles to objects. If you have an older version you can swap all of my dot notations with get and set calls, where appropriate.
Now with this initial plot, if we enter h.myaxes.XLim or get(h.myaxes, 'XLim'), we return:
ans =
1 10
Now if we zoom in arbitrarily and make the same call, we get something different. In my case:
ans =
3.7892 7.0657
Now it's up to you how you want to use this information to window your data. A very basic method would be to use find to get the indices of the closest data points to these limits.
For example:
newlimits = h.myaxes.XLim;
newminidx = find(x >= floor(newlimits(1)), 1);
newmaxidx = find(x >= ceil(newlimits(2)), 1);
newmin = x(newminidx);
newmax = x(newmaxidx);
Returns [newmin, newmax] of:
ans =
3 8
I used floor and ceil here because I know my data is integers, your criteria may be different but the process is the same. Hopefully this is enough to get you started.

How tick labels on one of the plot's axis can be multiplied in MATLAB?

I am currently trying to make 'nice looking' axis on my plot in Matlab. The values are samples where each is taken every 20ms. So on X axis I'd like to have time in ms. My general code for plotting data is very simple:
plot(1:size( data.matrix,1), data.matrix(:,1),'b');
So far I've been able to correctly display sample numbers on this axis:
Code:
set(gca,'XTick',(1:size(data.matrix,1))*20);
Result (a):
In the given example the number of samples equals 184, that makes:
time = 184 * 20ms = 3680ms
Because changing the XTick value didn't do much of a change, I've tested XTickLabel property:
Code:
set(gca,'XTickLabel',ceil(linspace(0,size(data.matrix,1)*20,10)));
Result (b):
Code:
set(gca,'XTickLabel',0:ceil((size(data.matrix,1)*20)/10):size(data.matrix,1)*20);
Result (c):
As you may see, the results aren't the worst one. But this is not what I am trying to achieve. My goal is to have values like this:
So it simply first plot's (result a) X axis multiplied by 20.
Can someone please tell me how to achieve this?
when you use :
plot(1:size( data.matrix,1), data.matrix(:,1),'b');
The first parameter 1:size( data.matrix,1) create a vector of consecutive integers representing the indices of your data points. Two things are wrong with that:
1) You do not need to specify it. Matlab plot function called with only the Y data will create this vector automatically and use it to plot the data.
2) These indices represent the position of your data in an array, they have no physical meaning (like "time" for your case).
The solution is to create a proper 'time' vector and send it as the first parameter to the plot function. This way Matlab will take care of the tick management and you don't have to temper with the manual xtick settings.
Look at the two ways of doing it in the example below and everything will be clear:
%// build a simple index "X" vector (as you were doing)
x_index = 1:size(data.matrix,1) ;
%// build a properly scaled time vector
timeInterval = 20 ; % in milliseconds
x_time = ( 0:size(data.matrix,1)-1 ) * timeInterval ;
plot( x_index , data.matrix(:,1) ) %// this will do like in your example
plot( x_time , data.matrix(:,1) ) %// this will do what you want
Can you not just do?
set(gca,'XTickLabel',(1:size(data.matrix,1))*20*20);

Getting the format correct when writing data to a text file from a matlab script

I'm trying to write data to a text file from my matlab script. I want two columns which it gives me but I want my time variable (t) followed by my variable (x) which is my amplitude. Its outputting a file like below.
Everything is perfect however i don't believe my (t) variable is first followed by my (x) variable. I'm trying to upload this file to ploy.ly to be graphed but when i set column 1 to my x variable and column 2 to my y variable it plots a oval like below. It's suppose to plot a sinusoidal signal.
My code is as follows:
f = 1E3;
T = 1/f;
tmin = 0;
tmax = 5*T;
dt = T/100;
t = tmin:dt:tmax;
x = sin(2*pi*f*t);
sinData.txt = fopen('sinData.txt','w');
fprintf(sample.txt,'%7.5f,%7.5f\r\n',x);
fclose(sinData.txt);
plot(t,x,'r');
grid on;
MATLAB writes files in column major format. What this means is that it will traverse every row first in one column before moving to the next column. If you have a M x 2 matrix of values where the first column is a vector of t values and the second column is a vector of x values, try transposing the matrix before writing to file. That way, each column represents a pair of (t,x) values for you and MATLAB should be able to preserve writing to the file so that each row is a unique pair.
Also, in your code you are only writing the x values to the file. This makes sense because if you look at your text file going line by line, it pretty much mimics a sinusoidal output. It goes from 0 up to 1, then back to 0 again. Your x vector is a sinusoidal output which thus fits the pattern. As such, you probably want to write the t values as well. To do this, modify the fprintf statement in your code so that it looks like this:
fprintf(sinData.txt,'%7.5f,%7.5f\r\n', [t;x]);
BUG SPOT FROM RTL (Thanks!): Make sure you change sample.txt to sinData.txt in your fprintf statement as that is the name of the handle to the open file that you have opened.
Looking at your code, t and x are row vectors, so this should (hopefully) work. I haven't tried it myself, and I'm somewhere where I don't have MATLAB accessible. Hit me up with a comment and let me know if it works.