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
采纳的回答
Star Strider
2018-10-26
编辑:Star Strider
2018-10-26
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 个评论
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.
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.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Text Files 的更多信息
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
