Problem while using textscan

My file has a string as the first row and numeric values as other rows, I can get rid of the header by using formatspec and N as given textscan help. I do not want to write %f four times while using the textscan again to read the numeric data. I have given the test code and please find the sample file. Thanks for helping
filename = 'S.txt'; fileID = fopen(filename); formatSpec = '%s'; N = 4; C_text = textscan(fileID,formatSpec,N); %% Read the numeric data in the file. C_data0 = textscan(fileID,'%f %f %f %f','CollectOutput',1); %% Here I do not want to write %f 4 times

 Accepted Answer

Star Strider
Star Strider on 26 Oct 2018
Edited: Star Strider on 26 Oct 2018
If you do not want to type the format string, just use the dlmread funciton:
S = dlmread('S.txt', '\t', 1, 0)
S =
1 1 1 1
2 2 2 2
3 2 3 3
There are many ways to read files in MATLAB.

18 Comments

Thanks a lot for the help, actually I am trying to read a very big file, which has around 300 columns and 1500 rows, I do not want to write %f in textscan as many as times, mentioned above. Your thing works but it gives me the error that The mismatch between file and format character vector, Trouble reading 'Numeric' field from file
My code worked with the example you gave.
If you have R2013b or later, the readtable (link) function is likely your best option. See the documentation on Access Data in a Table (link) to understand get your numeric data from it. Also see the table2array (link) function.
Thanks, I am attaching the file which I want to read. I do not want to read the first two rows although it will be helpful if I can get that information also.
This will do what you want:
BB_Table = readtable('BB.csv');
ColTitles = BB_Table.Properties.VariableNames; % Column Headers
NumericDataCell = table2array(BB_Table);
NumericData = str2double(NumericDataCell);
The ‘ColTitles’ variable is a (1x22) cell array of the header line column titles. (The readtable function alters them slightly to create valid MATLAB variable names from them.)
The ‘NumericData’ variable is a (1542x22) double array. It has several NaN elements, so consider that when you analyse the data.
Thanks a lot for the help.
As always, my pleasure.
Hi I wanted to write the above data in a file, using frintf will be very cumbersome if I have many columns, do you have any ideas.
Using writetable is likely the easiest. That is what I would use for your data.
Thanks a lot but I want to write the above NumericData (as suggested by you) into a file, I tried both dlmwrite and writetable but they both seem to give an error which says "Too many output arguments." Thanks
As always, my pleasure.
The writetable function does not have any output arguments. Its output is the file it creates.
Is there any way I can write above NumericData to a text file, ignoring the first row which is again a header.
There are several. Again, writetable offers several options in Write Table to Text File (link), as well as the dlmwrite (link) function, since these are all text files, and related functions. See the documentation on Text Files (link) for a full list.
Sorry but I tried and I am getting an error which says Error using dlmwrite Too many output arguments.
No worries.
The dlmwrite function does not have any output arguments. Its output is the file it creates.
Thanks but here is my code, which still gives an error which I do not understand, I am just trying to read the file and I want to write it again.
it gives an error which goes like this Undefined function 'real' for input arguments of type 'table'.
z = readtable('BB.csv');
dlmwrite('myFile.txt',z)
It is best to use writetable since you are writing a table to your output file.
Example
writetable(z, 'myFile.txt')
or:
writetable(z, 'myFile.csv')
depending on what you want to do. The function will create the correct output file depending on the file extension or the 'FileType' (link) name-value pair argument you specify, if you do not provide a file name extension.
Note I did not specifically test this. However, it should work.
Thanks a lot it helps
As always, my pleasure.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!