Calling a matrix cell with a variable
Mostrar comentarios más antiguos
I have a loop
for nLoop = 1:length(logfile)
ringfile=load([FolderName, '\ring_' logfile(nLoop,1) '.txt']);
end
And what I want to do here is to call an exact cell of my matrix, but I the row number to change with the loop.
Thanks
5 comentarios
Jan
el 7 de Ag. de 2013
The question is not clear. Please consider the difference between cell and "element of a matrix". More details are required to create an answer.
'change with the loop' how?
As long as you can compute the desired index or look it up from somewhere, sure...
for nLoop = 1:length(logfile)
ringfile=load([FolderName, '\ring_' logfile(nLoop) '.txt']);
[nr,nc]=size(ringfile); % find out how big it is..
aRandomLoc=ringfile(randi(nr),randi(nc)); % a random value
...
% a subset
the2ndCol = ringfile(:,2);
...
% walk thru by 2's...
for ic=1:2:nc
for ir=1:2:nr
rij = ringfile(ir,ic);
...
end
end
end
Do whatever with the results where the '...' are located.
If your use of "cell" means it is a cell array and is not referring to a memory location in an array, then use the {} curly brackets to dereference the cell contents. Search for "using cell arrays" in the online documentation for details of the nomenclature.
Azzi Abdelmalek
el 7 de Ag. de 2013
What do you mean by an exact cell of my matrix, which matrix? can you give a short example? What your file is containing?
Dimitar
el 7 de Ag. de 2013
dpb
el 7 de Ag. de 2013
Not a lot, no...but--
Sounds like it's simply logic on both the selection of a file and then some subset of data within that file -- both trivial enough if you have a way to define which you want. That's a piece of the puzzle you've got to have a generating mechanism for, whether it's asking the user interactively, reading from a separate file or having some logic internal that does so.
As for numbers from the first row of my matrix and put them in the file name in the places x and y, that's simply using sprintf to build a string and perhaps fullfile and/or friends to make a valid file name.
fn=sprintf('ring_%d_m_%y', v(ix,1:2));
filenew=fullfile('apath',[fn '.txt']); % a sample qualified name
v(ix,1:2)
above would be the ix-th row in your data array v read in earlier and in, say a loop on variable ix. I took the first two columns as purely an example to match the two fields needed in the sprintf call.
Salt the general ideas to suit your case as needed, of course...
Respuesta aceptada
Más respuestas (1)
Dimitar
el 8 de Ag. de 2013
0 votos
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!