You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
reading in text files
14 Comments
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.
Answers (1)
8 Comments
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
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)