开发者

Loops on a Matlab program

I have 3 sets of 10 vectors each, and I want to take 2 vectors from the first set , 2 vectors from the second set and 3 vectors from the third set . My goal is to make a loop to implement the following program, knowing that after each calculation, the开发者_运维问答 result shall be saved in a new file.

My problem is that I can not handle the indices included in the strings. I try to use multiple loops to scan the 3 sets in the order of indices. loops should contain the following program

clc;  
clear all;  

load('C:\Users\Documents\MATLAB\myFile\matrice_F.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_G.mat');  

F = m_F;  
G = m_G;  


load('C:\Users\Documents\MATLAB\myFile\matrice_J.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_K.mat');  

J = m_J;  
K = m_K;  

load('C:\Users\Documents\MATLAB\myFile\matrice_N.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_O.mat');  
load('C:\Users\Documents\MATLAB\myFile\matrice_P.mat');    
N = m_N ;  
O = m_O;  
P = m_P;  

[A,B,C,D,E] = myFun(F,G,J,K,N,O,P);  

file_name = 'matrice_final.mat';  
save(file_name,'A','B','C','D','E');   

I thank all those who responded to my question. Sorry if I was not as accurate from the outset. However, I would like the program looks something like:

clc; 


    clear all;    

    set1={F,G,FF,GG,X,Y,XX,L,BH,JK};  %set of name vectors  
    set2={J,K,HG,UY,TR,BC,XW,IOP,ES,QA};  %set of name vectors  
    set3={AJ,RK,DS,TU,WS,ZZE,ZXW,TYP,ZAA,QWW};  %set of name vectors  

for i=1:1:9    

        load('C:\Users\Documents\MATLAB\myFile\matrice_'set1(i)'.mat');    
        load('C:\Users\Documents\MATLAB\myFile\matrice_'set1(i+1)'.mat');   

        'set1(i)' = m_'set1(i)';     
        'set1(i+1)' = m_'set1(i+1)';     

    for j=1:1:9   

        load('C:\Users\Documents\MATLAB\myFile\matrice_'set2(j)'.mat');     
        load('C:\Users\Documents\MATLAB\myFile\matrice_'set2(j+1)'.mat');  

        'set2(j)' = m_'set2(j)';     
        'set2(j+1)' = m_'set2(j+1)';   

        for k=1:1:8   

            load('C:\Users\Documents\MATLAB\myFile\matrice_'set3(k)'.mat');       
            load('C:\Users\Documents\MATLAB\myFile\matrice_'set3(k+1)'.mat');      
            load('C:\Users\Documents\MATLAB\myFile\matrice_'set3(k+2)'.mat');        

            'set3(k)' = m_'set3(k)' ;      
            'set3(k+1)' = m_'set3(k+1)';     
            'set3(k+2)' = m_'set3(k+2)';      

    [Result1'index',Result2'index',Result3'index',Result4'index',Result5'index'] = myFun('set1(i)','set1(i+1)','set2(j)','set2(j+1)','set3(k)','set3(k+1)','set3(k+2)');  %% 9x9x8=648   index=1,2,...,648      

    file_name = 'matrice_final'index'.mat';      
    save(file_name,'Result1'index'','Result2'index'','Result3'index'','Result4'index'','Result5'index'');        
        clear 'set3(k)' 'set3(k+1)' 'set3(k+2)'   
        end   
    clear 'set2(j)' 'set2(j+1)'   
    end   
    clear 'set1(i)' 'set1(i+1)'
end


Note: Updated to meet the new requirements in the question...

Here's a solution that avoids using EVAL:

clc;
clear all;

set1 = {'F','G','FF','GG','X','Y','XX','L','BH','JK'};
set2 = {'J','K','HG','UY','TR','BC','XW','IOP','ES','QA'};
set3 = {'AJ','RK','DS','TU','WS','ZZE','ZXW','TYP','ZAA','QWW'};

matrices = cell(1,7);  %# An empty cell array
fileString = 'C:\Users\Documents\MATLAB\myFile\matrice_';  %# First part of
                                                           %#   file name

for i = 1:9  %# Loop over set1

  fileData = load([fileString set1{i} '.mat']);  %# Load file
  matrices{1} = fileData.(['m_' set1{i}]);       %# Get matrix

  fileData = load([fileString set1{i+1)} '.mat']);  %# Load file
  matrices{2} = fileData.(['m_' set1{i+1}]);        %# Get matrix

  for j = 1:9  %# Loop over set2

    fileData = load([fileString set2{j} '.mat']);  %# Load file
    matrices{3} = fileData.(['m_' set2{j}]);       %# Get matrix

    fileData = load([fileString set2{j+1)} '.mat']);  %# Load file
    matrices{4} = fileData.(['m_' set2{j+1}]);        %# Get matrix

    for k = 1:8  %# Loop over set3

      fileData = load([fileString set3{k} '.mat']);  %# Load file
      matrices{5} = fileData.(['m_' set3{k}]);       %# Get matrix

      fileData = load([fileString set3{k+1} '.mat']);  %# Load file
      matrices{6} = fileData.(['m_' set3{k+1}]);       %# Get matrix

      fileData = load([fileString set3{k+2} '.mat']);  %# Load file
      matrices{7} = fileData.(['m_' set3{k+2}]);       %# Get matrix

      [A,B,C,D,E] = myFun(matrices{:});  %# Pass matrices to your function

      index = k+8*(j-1)+72*(i-1);        %# Combination index
      fileName = ['matrice_final' int2str(index) '.mat'];  %# Save file name
      save(fileName,'A','B','C','D','E');                  %# Save the data

    end
  end
end

I made one small modification. Instead of saving A1 through E1 in file matrice_final1.mat, and A2 through E2 in file matrice_final2.mat, etc., I just saved A through E in each file, since the file name itself already indicates the index. If you want to load multiple files at once without the matrices A through E overwriting each other, you can do the following:

data1 = load('matrice_final1.mat');  %# Load data into structure data1
data2 = load('matrice_final2.mat');  %# Load data into structure data2

Now, data1.A will give you A for index 1, and data2.A will give you A for index 2.


Ok I think I get it now. You have having problems referring to the matrix loading from a mat file. You know the name of the matrix from the .mat file, but how do you refer to it in matlab?

You need to use eval

You'll need to do something like:

  1. Read the letter in the file name and store it in the variable letter
  2. Load the matrix
  3. Call loaded_array = eval(['m_', letter]);


Update:

OK, so your program looks good, but you have some basic errors:

String concatenation:

load('C:\Users\Documents\MATLAB\myFile\matrice_'set1(i)'.mat');

So the string you're passing to load isn't really a string until you concatenate it. For e.g., ['Hello' ' ' 'World'] will be the string Hello World. You need to enclose it in [ and ].

So change the load statements to:

load(['C:\Users\Documents\MATLAB\myFile\matrice_' set1(i) '.mat']);

Wrong use of eval

For eval to work, you need to construct the string (which is the command) and pass it to eval. The following will not work:

'set1(i)' = m_'set1(i)';

But this will work:

eval([ set1(i) ' = m_' set1(i) ';']);

Since it's the equivalent of (if i = 1);

A = m_A;

Use of index

I didn't understand the role of the index variable.

clear

clear can be used with string argument so:

clear 'set2(j)' 'set2(j+1)' 

Should be changed to:

clear(set2(j),set2(j+1));

Old Answer: You need eval. I think it's not recommended but it seems to be the way to go for you.

%%// Cleanup
clc;  
clear all;  

%%// The loop
matrices = 'FGJKNOP';
for i = 1:numel(matrices)
    load_string = ['load(''C:\Users\Documents\MATLAB\myFile\matrice_' matrices(i) '.mat'');'];
    eval(load_string); %// Example: load('C:\Users\Documents\MATLAB\myFile\matrice_F.mat'); 
    eval([matrices(i) ' = m_' matrices(i) ';']); %// Example: F = m_F;  

end

%%// Saving the data
results = 'ABCDE';
%// Make the string [A,B,C,D,E]
results_string = sprintf('%c,',results);
results_string = ['[' results_string(1:end-1) ']'];
%// Make the string (F,G,J,K,N,O,P)
matrices_args = sprintf('%c,',matrices);
matrices_args = ['(' matrices_args(1:end-1) ');'];    
%// Make the string ,'A','B','C','D','E' etc.
results_args = sprintf(',''%c''',results);

%// Save the data
eval([results_string ' = myFun' matrices_args]);
file_name = 'matrice_final.mat';
eval(['save(file_name' results_args ');']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜