Read folder files correctly into workspace and plot
5 views (last 30 days)
Show older comments
I have a folder with 20 tab delmitted txt files. The files tables of data generally in the form 200X19 (sometimes the number of rows varies). I am using uigetdir to select the folder of files and I am storing them in a datastore then using readall to extract the values into a table so I can then plot them.
When I extract the table I get a ~ 4000X19 table. I have to plot for example file 1 vs file 4 or maybe file 8 vs files 10 and 17. How do I either:
1) Loop through the files and load them in 1 by 1 to get 20, 200X19 tables that I can then plot as I need, or
2) Do this in a better way?
I have tried seperating them into structs but could only do so by variable name and so get the same issue but then I have 19, 4000X1 structs.
Code looks like this initially:
files = uigetdir('*.txt'); % Get all input files
ds = datastore(files, "VariableNamingRule","preserve");
DataTable = readall(ds);
Followed by this:
S = table2struct(data, "ToScalar",true)
A = S.Time_s_(2:1:198,:); % I went into the data to see where the files ended and began to set this
B = S.Temp_C_(2:1:200,:); % """"
plot(A,B)
I had also done this method straight from the table. I know this method isn't feasible for large data sets.
Any help will be greatly appreciated.
1 Comment
Mathieu NOE
on 17 Jul 2023
it's maybe very basic approach but why not simply declare which file is your "reference" file (like file 1) then define which other(s) file(s) you want to open and plot against the reference data ?
Answers (1)
Aman
on 25 Jul 2023
Hi,
I understand that you have a 20 tab delimited text file and want to plot out the data of a particular file against some other file.
To do the same, the approach of looping over the data will also work fine. But you can also do the same by following the below steps:
- Read the files using the “tabularTextDatastore” method, which will create a tabular text datastore.
- Read the data from the datastore in a cell array using the “read” method.
- Convert the cell array into a matrix using the “cell2mat” method.
- Use appropriate plotting function to get the plot of a file’s data against other files in the datastore.
Please refer to the following documentation to learn more about the methods discussed above.
- https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.tabulartextdatastore.html
- https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.read.html
- https://www.mathworks.com/help/matlab/ref/cell2mat.html
Hope it helps!
0 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!