Home > Enterprise >  using load function with variables in MATLAB
using load function with variables in MATLAB

Time:08-24

filterSize sz = sprintf( '%dx%d', filterSize, filterSize );

I have some .mat files named

results_3x3.mat

results_5x5.mat

and so on..

I am importing that files with load function. But since that I have 20 files I need to do it in a for loop. filterSize=3:2:41

I need to use the load function in MATLAB with variables. I am now doing it manually as follows:

F1_score_3 = load('results_3x3.mat');
accuracy_3 = F1_score_3.accuracy;

F1_score_5 = load('results_5x5.mat');
accuracy_5 = F1_score_5.accuracy;

F1_score_7 = load('results_7x7.mat');
accuracy_7 = F1_score_7.accuracy;

F1_score_3 = load('results_&s.mat',sz); doesn't work.

Can you help me with this? Also, can I define variables with another variable in them? Such as

F1_score_%d, filterSize

CodePudding user response:

Do not create dynamic variable names that contain numbers like this. They will be hard to work with downstream in your code. It would be better to store the results in an array or cell or struct. E.g., you could do something like this using a cell array:

F1_score = cell(41,1);
for filterSize=3:2:41
    fname = sprintf( 'results_%dx%d.mat', filterSize, filterSize );
    F1_score{filterSize} = load(fname);
end

Then when you want to get at accuracy, you can use indexing as usual. E.g.,

F1_score{3}.accuracy
  • Related