importing excel data using for loop

20 visualizaciones (últimos 30 días)
zachary schneider
zachary schneider el 19 de Ag. de 2019
Respondida: Star Strider el 19 de Ag. de 2019
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!

Respuesta aceptada

Star Strider
Star Strider el 19 de Ag. de 2019
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.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by