I have 100 .csv files that I want to import as 100 matrices into MATLAB. Each file has six columns and the first four rows are headers. I need each matrix to be imported as six columns without the first four rows.
Example code would be appreciated!
Thanks.
Very helpfull for such operations is the Matlab import tool which you will find in the Home tab and use it to generate a import function (Matlab-Code).
This tool allows you to select the data you need graphically and declare the data format and the variable names. Instead of importing this data directly, you can generate a import function. This function you can use to import all 100 .csv files in a loop. Further you can alternate the code for more detailed import settings.
(Detailed Explanation: https://www.mathworks.com/help/matlab/ref/importtool-app.html)
Hope this helps, cheers
Related
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.
I have 2D data for 10x10 matrices, and it looks like this
here is the table
However, data is updating and append every dt calculation, therefore I would like to reorganize and write it for specific columns , you may see this table in link
I use normally those codes to write
t=t+dt;
if ss==2000
dlmwrite('d:\Model_Results_Theta.txt', Tnew,'-append');
ss=0;
end
Could you recommend me any different way to organize the data based on the specific rows and columns using the matlab codes? Thanks in advance !!
I have a data file having 50 2-D data points written in Notepad. I want to use it in clustering algorithm to cluster these 50 points. How can I import this file? Is there any other way to use it in program?
You can save the data as a .csv file or you can save it to an excel spreadsheet and use xlsread(). See here for more info: http://www.mathworks.com/help/techdoc/ref/xlsread.html
For the .csv case, this post should prove helpful: Fastest way to import CSV files in MATLAB
Imagine you had the following data:
X = [randn(100,2)-1 ; randn(100,2)];
save data.mat X
Then its as simple as doing:
%# load data from MAT-file
load data.mat
%# cluster into K=2 clusters
C = kmeans(X,2);
%# show cluster assignment
gscatter(X(:,1), X(:,2), C)
It depends how you have formatted the data file. You say it is saved on notepad but that is not too helpful. Depending on what you have used as the data delimiter you can import the datafile into an array using the dlmread function. For example if your file is called filename.dat and have used a ; character to separate each data item within this file you could read the data into a matrix A using
A = dlmread("filename.dat",';');
I would suggest reading the help documentation on the dlmread function in matlab.
I'm currently writing a piece of code which is supposed to import text files using importdata and count the number of columns, i thought the cols() function would suffice for this, but it seems that all the imported data is stored as a double, meaning I can't perform this operation.
M=importdata('title');
numcols=cols(M.data);
Am I doing something wrong? I thought the data fromthe text file would be stored in a matrix/array?
Cols is a specific function for use in the database toolbox.
You probably just want to use the size function. EG:
size(M.data,2); %Returns number of columns
FYI
size(M.data, 1); %Returns number of rows.
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