Borrar filtros
Borrar filtros

Using loop in strings for import

2 visualizaciones (últimos 30 días)
Karl
Karl el 30 de Abr. de 2013
Respondida: Jacob Lynch August el 24 de Mzo. de 2014
I have excelfiles with names "2011", "2012", "2013" that i want to read from Matlab. I want to use a for loop reading these excelfiles, since tha names are so alike.
This did not work
for i = 2011:1:2013; xlsread('adress\(i).xlsx');
It does not seem like Matlab interprest the "i" in the string at a number, but onlye as an "i". Anybody know how to solve this?
  1 comentario
Alessandro Renna
Alessandro Renna el 6 de Mayo de 2013
You can also use
num2str(i)
to convert the numerical variable i in a string variable and then concatenate it to the string '.xlsx'
So:
for i=2011:1:2013
file{i}=xlsread([num2str(i) '.xlsx']);
end

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 30 de Abr. de 2013
Editada: per isakson el 30 de Abr. de 2013
The number, e.g. 2012, must be converted to a string. Note the double back-slash, "\\"
for ii = 2011:1:2013
[ num, txt ] = xlsread( sprintf( 'adress\\%i.xlsx', ii ) );
end

Más respuestas (1)

Jacob Lynch August
Jacob Lynch August el 24 de Mzo. de 2014
I have a similar problem. I have data taken from multiple machines with variable conditions, and a consistent heirarchy. E.g.
  • Machine = MAA, MAB, MAC, ..., MBH
  • Condition = 100, 200, 300, ..., 800
  • Part = Gold, Platinum, Silver
File = X:\Folder\Machine\Condition_Part\dat.csv
I want to treat text as I would a numerical list in a for loop. Obviously ['MAA' 'MAB' ... 'MBH'] would just concatenate the strings into one, which is not useful. I tried variations of the following and a few functions like strcat to name the file to no avail.
for machine = {'MAA' 'MAB' 'MAC' ... 'MBH'}
for condition = {'100' '200' '300' ... '800'}
for part = {'Gold' 'Platinum' 'Silver'}
CSV = ['X:\Folder\' machine '\' condition '_' part '\dat.csv']
end
end
What's the best way to generally go about this without elseif or cases?

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by