Read folder files correctly into workspace and plot
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Mathieu NOE
2023-7-17
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 ?
回答(1 个)
Aman
2023-7-25
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!