Upload multiple .mat files for processing

In MatLab, I (after an extended code run) have several .mat files output to .mat files. The actual matlab name for each .mat file is called results , but I used the save command to write them to different files. A small subset of files looks like this:

 results_test1_1.mat results_test1_2.mat results_test1_3.mat results_test1_4.mat results_test2_1.mat results_test2_2.mat results_test2_3.mat results_test2_4.mat 

Now I want to compare the results for each test, which means that I have to load all four .mat files and combine them in a graph. Reading in one file and creating the final graph is not a problem. But since all files have the same name matlab results , iterative loading is not an option (at least not the one I know about yet), since in the end only file 4 remains, since it overwrites the previous ones.

Is there a way to load all of these files and save them in different variables in the structure (for only one test suite)? Because all this by hand is a hell of a lot of work.

I tried using this method: Upload multiple .mat files to the Matlab workspace , but I get an Invalid field name error on loaded.(char(file)) = load(file);

+4
source share
1 answer

You can load into a variable (preferably an array of cells)

 results = cell( 2, 4 ); % allocate for testi=1:2 for resi = 1:4 filename = sprintf('results_test%d_%d.mat', testi, resi ); results{testi,resi} = load( filename ); end end 

Now you have all the results stored in the array of results cells, and you can access the stored variables, for example,

 results{1,3}.someVar % access variable someVar (assuming such variable was saves to the corresponding mat file 
+3
source

Source: https://habr.com/ru/post/1485488/


All Articles