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!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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 (한국어)