現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
reading in text files
14 件のコメント
I need 250 separate files that are 7909x10 doubles after entering. Doing the above loop I get 1 7909x34 double. I am just looking how to get these all in and then create another loop to reduce those data sets to the 250 individual files that have the data from columns 1,2,3 and 6 for all the 250 files I am importing.
You're only getting one file because you're overwriting M each time you read the next file. You need to index M. I would suggest using cell arrays.
M(k) = {dlmread(textFilename,' ',1,0)};
That combined them so I can see all 250 files so thank you. However, it is still creating a file much larger than the .txt file. the .txt file is originally 7909x10. The code creates a 1x250 cell which works but inside the cell the 250 data sets are 7909x34 doubles. Is there anyway to keep that 1x250 cell and extract only the columns that I need out of each individual 7909x34 double and create another 1x250 cell?
To the best of my knowledge dlmread(), or any importer command besides xlsread(), cannot import only specific columns. You're going to need to import then entire thing and then redefine the matrix to only include the desired columns. This can be done in a new matrix, or by just redefining the old.
M{k} = M{k}(:,[1,2,3,6]); % Reshape original, all data outside of columns % 1,2,3, and 6 will be lost N{k} = M{k}(:,[1,2,3,6]); % Make a new matrix, keep all of the original % data in M for later use
Awesome! thank you. Once I have done that I need to correct the two middle by a set value for each point, essentially normalizing them. I have tried something along the lines of
M(k) = {dlmread(textFilename,' ',1,0)}; N{k} = M{k}(:,[1,2,3,6]); T{k} = M{k}(:,[1,2+10000,3+10000,6]);
but that just moves those cells 10000 values higher, correct?
Anything contained within the parentheses following an array is the indexing. Using [1,2+10000,3+10000,6] is still specifying a location, so it will look for columns 1, 6, 10002, and 10003. If you want to just add values then you need to do so outside of the indexing operation.
T{k}(:,[1,4]) = M{k}(:,[1,6]); T{k}(:,[2,3]) = M{k}(:,[2,3]) + 10000;
Yes, it is possible to sort things based on specific values of rows. Going back to our previous example:
T{k} = T{k}(T{k}(:,4)==4||T{k}(:,4)==9||T{k}(:,4)==12||T{k}(:,4)==15||T{k}(:,4)==17,:); % Note that this removes all other values of T{k}. If you want to keep them % you need to save them as a different variable. Also note that all of the % right side of the equation is indexing, there is no value adjustment.
Basically, you can include logic challenges in your indexing.
回答 (1 件)
8 件のコメント
dinfo = 'C58501_line_ssF_%04d.txt'; pathToFiles = {dinfo.name};
ds = tabularTextDatastore(pathToFiles) ds.SelectedVariableNames = ds.SelectedVariableNames([1,2,3,6]);
while hasdata(ds) T = read(ds) % do stuff end
This returns an error of 'Struct contents reference from a non-struct array object'. Coming from the line
pathToFiles = {dinfo.name};
参考
カテゴリ
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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)