Problem while using textscan
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
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
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
2018년 10월 27일
Thanks a lot for the help.
Star Strider
2018년 10월 27일
As always, my pleasure.
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
2018년 10월 29일
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
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
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
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
2018년 10월 29일
Sorry but I tried and I am getting an error which says Error using dlmwrite Too many output arguments.
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
2018년 10월 31일
Thanks a lot it helps
Star Strider
2018년 10월 31일
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)
