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 件のコメント
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.
Star Strider
2018 年 10 月 27 日
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.
Shelender Kumar
2018 年 10 月 30 日
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)
Star Strider
2018 年 10 月 30 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
