importing excel data using for loop

3 次查看(过去 30 天)
I have been collecting data which is in a .xlsx format and am attempting to do some analysis using matlab. I have been trying to import all of the data using a single loop, but am having some issues with the syntax. I am trying to do:
for kk=1:24
f(kk,:) = readtable(sprintf('C:\Users\zschneider\Documents\MATLAB\Data\Point %d.xlsx', 'Sheet',1, 'Range','A10:A3024',kk));
end
In this line I am trying to accomplish reading table data for measurement point 1 to 24. I am able to use the readtable and get the data I want, I am just struggling to use an sprintf command to index the string element of the file name. Let me know if you have any questions or comments on a better way to do this. Thanks!

采纳的回答

Star Strider
Star Strider 2019-8-19
This variation on your loop will generate the names that you can then use for your readtable calls:
for kk=1:24
fv = sprintf('''C:\\Users\\zschneider\\Documents\\MATLAB\\Data\\Point %d.xlsx'', ''Sheet'',1, ''Range'',''A10:A3024''',kk);
fn{kk} = fv(2:end-1);
end
You need to be certain the file names this creates correspond to the actual file names with respect to the number of digits in the number and the spacing. Consider using %2d rather than %d if this is an issue.
Currently, this loop produces for ‘fn{1}’ and ‘fn{10}’:
'C:\Users\zschneider\Documents\MATLAB\Data\Point 1.xlsx', 'Sheet',1, 'Range','A10:A3024'
'C:\Users\zschneider\Documents\MATLAB\Data\Point 10.xlsx', 'Sheet',1, 'Range','A10:A3024'
I took the readtable calls out to test it. If those file names appear to be correct, use:
for kk=1:24
fv = sprintf('''C:\\Users\\zschneider\\Documents\\MATLAB\\Data\\Point %d.xlsx'', ''Sheet'',1, ''Range'',''A10:A3024''',kk);
f{kk} = readtable(fv(2:end-1));
end
to read your files.
I can’t test this with your files, so I am labeling it UNTESTED CODE, although I tested it as much as I could.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by