From: sscnekro on
Hi folks,

in a loop, I need to load a set of variables (of type matrix, double) and do some stuff using a function that takes the first variable and each of the remaining variables as argument. It would be fine to take it in a loop.

Normally, I use S = load('filename', 'apple', 'cherry', 'kiwi' ... ) However, if I understand the hlp doc correctly, in structures I need to refer to variables by explicitly writing their names, S.apple, etc. This seems not helpful for taking them in a loop. But maybe I am wrong, I really don't know much on it.

% the problem rephrased is quasi
for ii = 1:endval
load variables 'apple', 'cherry', 'kiwi'... from (sprintf('filename%d.mat', ii)
for apple and each of cherry, kiwi, etc
results = somefunction(apple, the other variable, ...)
end
save results to (sprintf('filename%d.mat', ii)
end

Can you please give a hint how best to proceed? Shall I use cell arrays for loading the stuff?? and how? Please help, I need to learn how to do this type of task properly (and it's quite urgent). Thanks!!!
From: Jan Simon on
Dear Sscnekro,

> Normally, I use S = load('filename', 'apple', 'cherry', 'kiwi' ... ) However, if I understand the hlp doc correctly, in structures I need to refer to variables by explicitly writing their names, S.apple, etc. This seems not helpful for taking them in a loop. But maybe I am wrong, I really don't know much on it.

Can you describe why "this seems not helpful for taking them in a loop"?
If you've saved two variables to a MAT file, you can load one of the variables either with:
S = load(MatFileName);
or
S = load(MatFileName, NameOfVariable1)
In the 1st case S contains both variables as fields, in the 2nd S has the needed field only.
If you call LOAD with output, the variable is inserted in the workspace directly, which might be nicer and/or a source of unexpected problems depending on the contents of the file:
clear all
load(MatFileName)
disp(NameOfVariable1);

It depends on your needs and taste.

Good luck, Jan
From: sscnekro on
Hi, Jan, thanks for your reply, nice to hear from you.

> Can you describe why "this seems not helpful for taking them in a loop"?

Okay, I hope now it will be more clear: The point is, even if you forget the stuff about loading them, still I need to write the loop
for apple and each of cherry, kiwi, etc ; results = somefunction(apple, the other variable, ...); end;
If they were stored in a cell, I would take apple aside and iterate the loop over the remaining cells, like somefunction(apple, cell{ii}). I don't know how something similar could be done for structures. I just explained the whole example including the load stuff so that the readers who know to help can see, where I need to go with that, as everything starts with the load. Thank you.
From: Oleg Komarov on
"sscnekro " <in(a)mail.net> wrote in message <hvvdnt$eae$1(a)fred.mathworks.com>...
> Hi, Jan, thanks for your reply, nice to hear from you.
>
> > Can you describe why "this seems not helpful for taking them in a loop"?
>
> Okay, I hope now it will be more clear: The point is, even if you forget the stuff about loading them, still I need to write the loop
> for apple and each of cherry, kiwi, etc ; results = somefunction(apple, the other variable, ...); end;
> If they were stored in a cell, I would take apple aside and iterate the loop over the remaining cells, like somefunction(apple, cell{ii}). I don't know how something similar could be done for structures. I just explained the whole example including the load stuff so that the readers who know to help can see, where I need to go with that, as everything starts with the load. Thank you.

with dynamic field indexing:

myFnames = fieldnames(yourStruct);
struct.(myFnames{jj})

Oleg
From: sscnekro on
> myFnames = fieldnames(yourStruct);
> struct.(myFnames{jj})

Okay, perfect, I see now!!! Thanks for saving my code (SMC) !!! :O)