Calling a matrix cell with a variable

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 Kommentare

Jan
Jan am 7 Aug. 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.
dpb
dpb am 7 Aug. 2013
Bearbeitet: dpb am 7 Aug. 2013
'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
Azzi Abdelmalek am 7 Aug. 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
Dimitar am 7 Aug. 2013
Sorry for the unclear question. What I need to do is to get a file with some name, say ring_1_m_3 from a list of files ring_x_m_y. However I have to get just a few specific files from all my data. And the values for x and y are listed in a matrix, for example: [1 1, 1 2, 1 3, 2 1..]. And what I want to do is get the numbers from the first row of my matrix and put them in the file name in the places x and y, after that do some operations with this file and go back at the begging to call the numbers from the second col and so on.
I hope this makes it clearer. Thanks
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...

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 7 Aug. 2013
Bearbeitet: Jan am 7 Aug. 2013

0 Stimmen

index = [1 1; 1 2; 1 3; 2 1];
for iFile = 1:size(index, 1)
name = sprintf('ring_%d_m_%d.txt', index(iFile, :));
ringfile = load(fullfile(FolderName, name));
end

Weitere Antworten (1)

Dimitar
Dimitar am 8 Aug. 2013

0 Stimmen

Thank you both Jan and dbp and all who tried to help. Sorry about the confusion, but I am new to Matlab. The problem wasn't calling data from my matrix, but not making it num2str when I try to load my files.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 7 Aug. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by