IN MATLAB: How to plot a graph every 3 lines from a Text file? - matlab

I have a large text file with 2 columns where the values are separated by commas. I am trying to create a simple program which allows to plot a graph with the data extracted for every 3 rows consecutively until reaching the end of the file.
The first 9 rows of my file can be seen below:
115,1.2
324,3.4
987,1.2
435,-2.3
234,1.4
278,1.3
768,3.4
345,-1.3
126,3.6
I have been reading that with 'Textread' I can write my data into multiple outputs and then I can use 'plot', to plot the previous generated outputs on a graph. I know that I will need some loop whiles to repeat the process and indicate the end of the file, etc. But I am struggling to find the way to do this:-(.
I have only managed plotting a graph for the first 3 rows of my file (see code below), but I need this process to be repeated until the end of the file.
[Codes,Values]=textread('MyData.txt','%3u %f',3,'delimiter',',','emptyvalue',NAN); %//Reads the first three rows of my file and stores the values in 2 variables
figure
plot(Codes,Values) %//plots a graph with the variables obtained before
saveas(gcf,'Graph.pdf') %//Saves the created graph in a pdf format file.
I would be very grateful if somebody could help me.

I have finally found out a way. I paste here the code that allows me plotting a graph every three lines from my text file.
[Codes,Values]=textread('MyData.txt','%3u %f','delimiter',',','emptyvalue',NAN); %//Reads my text file and stores the values in 2 variables
nrows=1;
conta=1;
contb=3;
contc=1;
contd=3;
nfig=1;
while nrows<=size(Codes,1)
if contb<=size(Codes,1)
Codes1=Codes(conta:contb,1);
Values1=Values(contc:contd,1);
figure
plot(Codes1,Values1) %//plots a graph with the selected rows.
saveas(gcf,strcat('Graph', num2str(nfig),'.pdf')) %//Saves the created graph in a pdf format file.
else
Codes1=Codes(conta:contb,1);
Values1=Values(contc:contd,1);
figure
plot(Codes1,Values1) %//plots a graph with the selected rows.
saveas(gcf,strcat('Graph', num2str(nfig),'.pdf'))
end
nrows=nrows+3;
conta=conta+3;
contb=contb+3;
contc=contc+3;
contd=contd+3;
nfig=nfig+1;
end

You mentioned that it is a large text file, but will it an issue to load the whole text file and then simply sample every third row, for example, as follows:
[Codes, Values] = textread('MyData.txt','%3u %f','delimiter',',','emptyvalue',NAN);
rowstokeep = 1:3:length(Values) % every third line
plot(Codes{rowstokeep}, Values{rowstokeep}); % plot just the rows you wanted

Related

MATLAB: making a histogram plot from csv files read and put into cells?

Unfortunately I am not too tech proficient and only have a basic MATLAB/programming background...
I have several csv data files in a folder, and would like to make a histogram plot of all of them simultaneously in order to compare them. I am not sure how to go about doing this. Some digging online gave a script:
d=dir('*.csv'); % return the list of csv files
for i=1:length(d)
m{i}=csvread(d(i).name); % put into cell array
end
The problem is I cannot now simply write histogram(m(i)) command, because m(i) is a cell type not a csv file type (I'm not sure I'm using this terminology correctly, but MATLAB definitely isn't accepting the former).
I am not quite sure how to proceed. In fact, I am not sure what exactly is the nature of the elements m(i) and what I can/cannot do with them. The histogram command wants a matrix input, so presumably I would need a 'vector of matrices' and a command which plots each of the vector elements (i.e. matrices) on a separate plot. I would have about 14 altogether, which is quite a lot and would take a long time to load, but I am not sure how to proceed more efficiently.
Generalizing the question:
I will later be writing a script to reduce the noise and smooth out the data in the csv file, and binarise it (the csv files are for noisy images with vague shapes, and I want to distinguish these shapes by setting a cut off for the pixel intensity/value in the csv matrix, such as to create a binary image showing these shapes). Ideally, I would like to apply this to all of the images in my folder at once so I can shift out which images are best for analysis. So my question is, how can I run a script with all of the csv files in my folder so that I can compare them all at once? I presume whatever technique I use for the histogram plots can apply to this too, but I am not sure.
It should probably be better to write a script which:
-makes a histogram plot and/or runs the binarising script for each csv file in the folder
-and puts all of the images into a new, designated folder, so I can sift through these.
I would greatly appreciate pointers on how to do this. As I mentioned, I am quite new to programming and am getting overwhelmed when looking at suggestions, seeing various different commands used to apparently achieve the same thing- reading several files at once.
The function csvread returns natively a matrix. I am not sure but it is possible that if some elements inside the csv file are not numbers, Matlab automatically makes a cell array out of the output. Since I don't know the structure of your csv-files I will recommend you trying out some similar functions(readtable, xlsread):
M = readtable(d(i).name) % Reads table like data, most recommended
M = xlsread(d(i).name) % Excel like structures, but works also on similar data
Try them out and let me know if it worked. If not please upload a file sample.
The function csvread(filename)
always return the matrix M that is numerical matrix and will never give the cell as return.
If you have textual data inside the .csv file, it will give you an error for not having the numerical data only. The only reason I can see for using the cell array when reading the files is if the dimensions of individual matrices read from each file are different, for example first .csv file contains data organised as 3xA, and second .csv file contains data organised as 2xB, so you can place them all into a single structure.
However, it is still possible to use histogram on cell array, by extracting the element as an array instead of extracting it as cell element.
If M is a cell matrix, there are two options for extracting the data:
M(i) and M{i}. M(i) will give you the cell element, and cannot be used for histogram, however M{i} returns element in its initial form which is numerical matrix.
TL;DR use histogram(M{i}) instead of histogram(M(i)).

How to plot .txt file

I have a text file with two columns, called Fx1. In column 1 it shows deflection calculations and in column 2 it shows force calculation (about 100).
I loaded the text file into matlab as a variable, called Fx1.
How do I plot this text file as a graph, with deflection as my x value, and force as my y value? Apparently, I am supposed to define my variables, but I do not know how to do so when I'm getting the data from a .txt file.
This is what I did and I did not get the correct graph:
plot(fx1)
Any ideas?
Here is a screenshot of my text file:
Here is my text file. It continues for many values, I just copy and pasted the beginning
Here is a screenshot of my whole workspace, I am trying to make a plot for all txt files between Fx1-Fx9.
Matlab work space
You should separate fx1 into two vector. Use these commands:
x=fx1(:,2)';
y=fx1(:,1)';
plot(x,y);
In additional you can combine these three command to one command:
plot(fx1(:,2)',fx1(:,1)');

how to select or load between different tables in workspace

I have a *.mat file with some tables included.I want to load each table one by one and then plot it so that the figures of each table update on one single figure .Please tell me if i am not clear ...I though this code must have been enough but it is not working when i asked it to load one table ... please guide me why and how i should improve the code ?
Thank you
filename = uigetfile
load(filename)
c=who('Hs*' ) % all the tables which starts with Hs
cc=numel(c) % count the number of desired tables in MAT file which we loaded before
for i=1: cc
b=load(filename,c(i));
contour(b,60)
end
I can see at least three errors in your code.
After the first load you don't clear the mat file, and hence all tables are still in the memory.
c is a cell array, and you shoud index it by using brackets (i.e. c{i}).
By default each plot in a figure deletes the previous one. Writing figure(); hold on; before the for loop shoud fix your problem.

gmdistribution.fit and .mat files

I've a very large .mat file which contains a lot of data which I need to visualize. .mat file contains 5 row with each row containing 1x5 matrix - which contains the data. I need to concatenate specific rows together, then apply gmdistribution.fit to it. I'm not sure as to how exactly I access specific elements of the .mat file to concatenate them together.
Say I wish to concatenate first row - > 1st row with 2nd row - > 1st row. How would I go about doing this? I'm new to matlab and finding it difficult to grasp it.
Also, could you explain gmdistribution.fit, please? I read the documentation in their website, however, I still am not exactly sure about the parameters.
Thankyou for your help.
To access first row:
matrix(1);
To access second row:
matrix(2);
To vertically concatenate 1st and 2nd rows into a new matrix:
newMatrix = [matrix(1) ; matrix(2)];
And you can do this with any row in your matrix.
As for gmdistribution.fit, it is just trying to fit your matrix to a Gaussian distribution. Without a more specific question, all I can do is point you to the documentation, which holds and explains all the parameters.

Error while loading xls file in MATLAB

I am trying to load an xls file in MATLAB. The xls file contains numerical values. I have successfully loaded and plotted the file but if I change the dimensions of the file (i.e. number of rows and number of columns) there is an error:
Numeric = xlsread('Test_results_new')
??? No appropriate method or public field UsedRange for class Interface.Microsoft_Excel_12.0_Object_Library._Chart.
Please note that when I make a change in the xls file I also make changes in the MATLAB code for number of rows and columns.
Can someone help me?
I had the same problem recently after months of it continuously working, I found out it was because I had made a plot in the excel file, so (i think) when xlsread was called it was looking at the first sheet (the plot instead of the table) and obviously could not read it. You can specify which sheet to look at with xlsread, just type help xlsread and it will explain how