Matlab how to open file in excel put into array and convert numbers to units of measurement - matlab

I have an excel file to open matlab and put into an array of cells - then take the numbers in the cells and convert a few measurements. how do i do this

I'd suggest looking into the two functions xlsread and xlswrite These both handle input (from an .xls or .xlsx) file to matlab and output from matlab to an excel file, respectively. If you're looking to do something different than that, please elaborate a bit more than what you've posted.

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)).

Writing matlab vector to a file that is matlab readable

For matlab: Is there a way to write the value of a vector to a file that can later be opened and read by another matlab program?
Specifically: I have a matlab program that computes a binary-valued vector $zvector$ with 10^7 entries. I want to write $zvector$ as data to an output file so that it can be emailed and easily read as input to another matlab program. Ideally, the output file would be called “Output.m” and would look like:
zvector=[
0
1
1
…
0
1
];
I like the .m format because it is easy to use for matlab input. I have experimented with matlab’s write() and fwrite() commands, with no success. I observe that these generate files that cannot be easily read as matlab-recognizable inputs (at least, I do not know how to read from them). Is there a way to accomplish my goals? Thanks.
PS: I am interested in the easiest way. If this involves a different type of file format (not a .m format) that is fine. However, in that case, can you provide both the writing and reading commands? Thanks again.
Thanks to #edwinksl for pointing me in the right direction with MAT files. I do not know the accepted practice here, but in stackexchange math it is encouraged to answer your own question if a hint from comments got you all the way there. So I will answer my own question.
The Mat format does this well. Here are example script files for reading and writing in the Mat format (see also links in above comments for more documentation):
***Script file OutputTest.m:
filename = 'TestFile.mat';
TestVector=[1 1 0 1];
save(filename, 'TestVector');
***Script file IntputTest.m
filename = 'TestFile.mat';
file=load(filename);
z =file.TestVector;
z

matlab cannot read text file containing ^* as power of 10

I need to read text files into Matlab. In the text files there are numbers like 5.875489^*-6, which is indeed 0.000005875489. Matlab cannot read this format and since there are too many files, I cannot change the format in all the files manually. So, I wonder if there is any tips to make Matlab reading the files as they are?
Any help and guide is highly appreciated.
Marilla.
As pointed out by #vu1p3n0x, it would probably be easier to replace ^* by e using a replace-all. Alternatively, if that is unpractical, you could read in the the mantissa and exponent separately and perform the exponentiation in Matlab:
Raw = textscan(fid, '%f^*%f');
Result = Raw{1}.*10.^Raw{2};

How to deal with large Excel file using MATLAB?

I'm trying to deal with an Excel file which comprises 1 million rows. However, when I open it in MATLAB, only 10,000 rows are displayed.... Could anyone tell me how to import full data using MATLAB?
Type at your MATLAB Command Window:
[~,~,data] = xlsread('X:\path to your file\excel file.xls');
where you should replace the xlsread argument with something that's suitable. The result will be a cell array of mixed numeric (if they could be converted) and strings read from the first sheet in the file. First cell in data corresponds to the upper/left cell in the worksheet.
If you want to specify the sheet, or for more refined function call, read the function's help.
NB
Some of the ways to call the xlsread function are possible or not depending on whether you have Office installed or not.

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