0

I currently have a MATLAB function that looks like this:

function outfile=multi_read(modelfrom,modelto,type)

models=[modelfrom:1:modelto];
num_models=length(models);

model_path='../MODELS/GRADIENT/'

for id=1:num_models
    fn=[model_path num2str(models(id)) '/'];  %Location of file to be read
    outfile=model_read(fn,type);              %model_read is a separate function
end
end

The idea of this function is to execute another function model_read for a series of files, and output these files to the workspace (not to disk). Note that the output from model_read is a structure! I want the function to save the file to the workspace using sequential names, similar to typing:

file1=multi_read(1,1,x)
file2=multi_read(2,2,x)
file3=multi_read(3,3,x)
etc.

which would give file1, file2 and file3 in the workspace, but instead by recalling the command only once, something like:

multi_read(1,3,x)

which would give the same workspace output.

Essentially my questions is, how do I get a function to output variables with multiple names without having to recall the function multiple times.

3
  • I think you can use the eval function in a loop. Commented Nov 19, 2012 at 10:58
  • 3
    Why not using a cell array? It gives you exactly what you need, but wrapped in one object. You will retrieve the outputs of multi_read by doing file{i}. Commented Nov 19, 2012 at 10:58
  • Apologies to those who have replied, but I should have mentioned that the output from the model_read funtion is a structure array. I have edited the question to reflect this. Commented Nov 19, 2012 at 12:35

2 Answers 2

2

As suggested in the comment I would try this approach which is more robust, at least IMHO:

    N = tot_num_of_your_files; %whatever it is          

    file = cellfun(@(i)multi_read(i,i,x),mat2cell(1:N,1,ones(1,N)),...
                         'UniformOutput' , false); %(x needs to be defined)

You will recover objects by doing file{i}.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 for avoiding eval and providing a much cleaner solution.
Why do you mention that this approach is more robust?
@DennisJaheruddin, more robust with respect to what the OP suggests. Having N different variables with indexed names doesn't look robust to me. If N is big you need cycles to interact with such variables and thus run-time mechanisms to obtain the variable names.
As per my amendments above, note that the output from model_read is a structure array. I can't seem to port your answer above to work with a structure array as output. For reference, N is ~50.
@heds1, the point still holds; cells are suitable to store heterogeneous data types. In other word each cell entry can be different, e.g. File{1} can be a struct and File{2} can be a matrix and so on, thus you can have structs in them. Does this look ok to you?
1

Here is code to do what you ask:

for i = 1:3
   istr=num2str(i)
   line = ['file' istr '= multi_read(' istr ', ' istr ', x)']
   eval(line)
end

Alternatively, here is code to do what you should want:

for i = 1:3
    file{i} = multi_read(i,i,x)
end

1 Comment

Thanks for your suggestions, but I am trying to avoid the use of eval. Second answer also works, but I think the one-liner of @Acorbe is more useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.