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

 채택된 답변

Star Strider
Star Strider 2018년 10월 26일
편집: Star Strider 2018년 10월 26일

0 개 추천

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

Shelender Kumar
Shelender Kumar 2018년 10월 27일
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
Star Strider
Star Strider 2018년 10월 27일
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.
Shelender Kumar
Shelender Kumar 2018년 10월 27일
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.
Shelender Kumar
Shelender Kumar 2018년 10월 27일
Thanks a lot for the help.
Star Strider
Star Strider 2018년 10월 27일
As always, my pleasure.
Shelender Kumar
Shelender Kumar 2018년 10월 29일
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.
Star Strider
Star Strider 2018년 10월 29일
Using writetable is likely the easiest. That is what I would use for your data.
Shelender Kumar
Shelender Kumar 2018년 10월 29일
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
Star Strider
Star Strider 2018년 10월 29일
As always, my pleasure.
The writetable function does not have any output arguments. Its output is the file it creates.
Shelender Kumar
Shelender Kumar 2018년 10월 29일
Is there any way I can write above NumericData to a text file, ignoring the first row which is again a header.
Star Strider
Star Strider 2018년 10월 29일
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.
Shelender Kumar
Shelender Kumar 2018년 10월 29일
Sorry but I tried and I am getting an error which says Error using dlmwrite Too many output arguments.
Star Strider
Star Strider 2018년 10월 29일
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.
Shelender Kumar
Shelender Kumar 2018년 10월 31일
Thanks a lot it helps
Star Strider
Star Strider 2018년 10월 31일
As always, my pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

태그

질문:

2018년 10월 26일

댓글:

2018년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by