Borrar filtros
Borrar filtros

concatenating string variable names and passing on values of a matrix in a for loop

7 visualizaciones (últimos 30 días)
Hi there,
I have many files, for each file I create variable names with part of the filename (using for loop and counter the filecounter) and then, I pass on to them the values of a matrix (AWFR) I create for each iteration, using eval()
The problem is that I want to concatenate all these column - vector variables into one large column vector, without having to type the names manually all over again, after these are saved in the workspace.
I managed to create a matrix containing all the names of the variables (AWFRallthenames), but I couldn't pass on the values with eval([AWFRallthenames(filecounter,:),'=AWFR']).
Is there any other way and why the above does not work? I am attaching a part of the code if that helps!!
Any help appreciated! Thank you
for filecounter=1:2%length(FileList)
.......
datehour=BasicName(end-12:end)
onoma1='AWFR'
onoma2=datehour
AWFRname=horzcat(onoma1,onoma2)
% matrix allthenames keeps all the AWFR names (every hour,
%every .xdat file), in integer values
allthenames(filecounter,:)=char(AWFRname)
%and this converts the integer values to a string
AWFRallthenames=char(allthenames)
%Now create the AWFR matrix. NO stimulation intervals included.
AWFR = zeros(nBins, 1);
for n=1:nBins
index_ofeach_bin=find( (Ts>(n-1)*(Fs*60*BinSize)) & (Ts<=n*(Fs*60*BinSize)))
AWFR(n)=length(index_ofeach_bin)
end
%now I pass on the values to the string name I have created before (AWFRname)
%eval([AWFRname,'=AWFR'])
%now I pass on the values of the string name I have created before, and
%rests in a row of the AWFRallthenames matrix
eval([AWFRallthenames(filecounter,:),'=AWFR'])
  3 comentarios
Stelina
Stelina el 12 de Mayo de 2014
Editada: Stelina el 12 de Mayo de 2014
Hey Geoff,
Yes indeed the initial way I was trying to solve this was by keeping the integer values of the strings in the filecounter row of AWFRallthenames and then using char()to get the string names, but it produced the same results with eval([AWFRname],'=AWFR')
@dpb thanx for the links

Iniciar sesión para comentar.

Respuesta aceptada

Roberto
Roberto el 8 de Mayo de 2014
Creating variable names via EVAL function is highly NOT RECOMMENDED. Try to use a different philosophy... try this for example:
% create your 'myVar' structure with all contents to store:
for fCounter = 1 : totalFiles
myVar(fCounter).datehour = BasicName(end-12:end) ;
myVar(fCounter).onoma1 = 'AWFR' ;
myVar(fCounter).onoma2 = myVar(fCounter).datehour ;
myVar(fCounter).AWFRname = horzcat(myVar(fCounter).onoma1,myVar(fCounter).onoma2) ;
myVar(fCounter).AWFR = zeros(nBins, 1);
for n=1:nBins
index_ofeach_bin = find( (Ts>(n-1)*(Fs*60*BinSize)) & (Ts<=n*(Fs*60*BinSize)));
myVar(fCounter).AWFR(n) = length(index_ofeach_bin);
end
end
% Process all elements
for i = 1 : numel(myVar)
disp(['AWFR name: ' myVar(i).AWFRname]);
disp(myVar(i).AWFR);
end
% then you can save only one variable to workspace
save('myfile.mat','myVar');
In case you want to learn how to filter structures, to get just one element, have a good look in to this amazing video tutorial. Good luck!
  1 comentario
Stelina
Stelina el 12 de Mayo de 2014
Hey Roberto,
Thanx a lot both for the tutorial and the code!
Indeed, I made a structure, where I added two fields, one for keeping the names and the other the values, so I can concatenate easily. Much more straightforward this way!
Stelina

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by