Matlab Importdata - 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.

Related

Is there a native function in Matlab that export string array to csv and vice versa?

Now that string array is a thing since R2016b, is there a native function that export a string array to csv file and vice versa?
A function that fills the same role of csvread and csvwrite for numeric arrays in the old days but for string arrays. And to relax the requirement, say the string array contains columns of pure strings and columns of pure doubles. Stock prices with time stamp strings would be an example.
Native = not looping with fprintf. But if you are certain Matlab hasn't included any such functions yet, feel free to answer with the best approach thusfar without any restrictions.
Without any native function, pre-R2013a, looping with fprintf is the only way I can think of. And it was awful. Given past reputation of inefficiency, I still don't trust looping in Matlab.
Post-R2016b, one can convert a string array to cell array with num2cell and then to table with cell2table. Table can be written to csv file with writetable. This is actually fast, as writetable is fast. Only num2cell slows down the whole process a little. However, formatting is impossible along the way.
Post-R2019a, cell2table can be skipped with writecell, which is nice but the time consuming (slightly) step is num2cell and formatting should still be impossible. (I don't have R2019a to test it.)
Is there a better way or is it another one of those basic things left to be desired about Matlab?
writematix and readmatrix are the functions to do that since R2019a.
%If S1 is a string array that you want to `foobar.csv` then:
writematrix(S1,'foobar.csv');
%To read this csv file back into MATLAB as the same string array, use:
S2 = readmatrix('foobar.csv','OutputType','string');
%Verifying the result:
isequal(S1,S2)
ans =
logical
1
Loops have been significantly improved since R2015b. Not all loops are slow and not all vectorised versions are faster. The correct approach is to timeit when in doubt.

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.

Simplest way to read space delimited text file matlab

Ok, so I'm struggling with the most mundane of things I have a space delimited text file with a header in the first row and a row per observation and I'd like to open that file in matlab. If I do this in R I have no problem at all, it'll create the most basic matrix and voila!
But MATLAB seems to be annoying with this...
Example of the text file:
"picFile" "subjCode" "gender"
"train_1" 504 "m"
etc.
Can I get something like a matrix at all? I would then like to have MATLAB pull out some data by doing data(1,2) for example.
What would be the simplest way to do this?
It seems like having to write a loop using f-type functions is just a waste of time...
If you have a sufficiently new version of Matlab (R2013b+, I believe), you can use readtable, which is very much like how R does it:
T = readtable('data.txt','Delimiter',' ')
There are many functions for manipulating tables and converting back and forth between them and other data types such as cell arrays.
There are some other options in the data import and export section of the Statistics toolbox that should work in older versions of Matlab:
tblread: output in terms of separate variables for strings and numbers
caseread: output in terms of a char array
tdfread: output in terms of a struct
Alternatively, textscan should be able to accomplish what you need and probably will be the fastest:
fid = fopen('data.txt');
header = textscan(fid,'%s',3); % Optionally save header names
C = textscan(fid,'%s%d%s','HeaderLines',1); % Read data skipping header
fclose(fid); % Don't forget to close file
C{:}
Found a way to solve my problem.
Because I don't have the latest version of MATLAB and cannot use readable which would be the preferred option I ended up doing using textread and specifying the format of each column.
Tedious but maybe the "simplest" way I could find:
[picFile subCode gender]=textread('data.txt', '%s %f %s', 'headerlines',1);
T=[picFile(:) subCode(:) gender(:)]
The textscan solution by #horchler seems pretty similar. Thanks!

xlsread function with very small number matlab

I want to use xlsread function in matlab to import very small data like 10E-13. But it always shows 0 in vector 'num'. I want to read the exact number and export it.
So, does anyone know how to increase the accuracy or precision?
Thank you in advance!
You can't change the precision with which xlsread reads the data. However, the output array might actually contain the data in num, but MATLAB displays it as 0. Run format long g, then diplay it again.

dlmwrite for specific rows and columns in matlab

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 !!