1

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

19 Comments

Thank you but I get an error saying that >Dot indexing is not supported for variables of this type.
I don't have your mat files so I can't know what variables and types you are loading. Once you do the load, examine the F1_score{3} variable to see what was loaded etc.
Isn't F1_score{3} a struct? Doesn't it have a field named accuracy?
What is class(F1_score) and class(F1_score{3})?
So doesn't that mean that class(F1_score{3}) is a struct? What does fieldnames(F1_score{3}) show?
|

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.