Save and load variables of different samples
3 views (last 30 days)
Show older comments
I do experiments with different samples (let's say 10 different materials). After measuring all samples seperatly, I apply a Matlab Code(the same code for all samples) to the data of each sample. The code does some calculations and in the end I have some variables. These variables I would like to load into another Matlab Code which is suppose to plot some of the variables (e.g. comparison plot for different samples, etc.)
Now the question: How can I save and load the variables in a way to access the variables of different samples (e.g. plot variable 1 of sample 1 with variable 1 of sample 2)?
My idea was to add all the variables to a structure named after the sample and save the structure (e.g water.density, water.color, etc. and then save('filename.mat','-struct','water') ). But the problem is that I am not able to name the structure efficiently (I need to change the code every time). Is there a better approach for this problem?
11 Comments
Stephen23
on 14 Apr 2021
"Is this the most elegant way"
It looks like a good start to me. The real questions are:
- does it do what you want?
- is it easy for you to understand and maintain?
As mentioned in a previous comment, and for the reasons explained in more detail here
it would be best to not put lots of (particularly numeric) meta-data together into one string. You will regret that later.
"But how can I plot the same thing in the case I do not now which index belongs to the sample 'milk 10g/l' or in the case in which I add samples to the structure A again and again and the indices are thus shiftet each time (and I would need to change the number in the plot command each time)?. How can I easily do this without writing a lot of code(In the real experiment I have a lot of samples and want to plot way more than simply the conductivity for one sample)?"
Take a deep breath. Let the code do these things for you. Do not write lots of code (that is what loops are for).
In essence, everything boils down to indexing.
So you want to plot milk vs coffee: then use strcmp (or whatever) to get the indices of those names. And then loop over all concentrations (stored as numeric, of course) and plot each pair of corresponding data sets. Make sure your code can cope with exceptions (missing pairs, no matches, etc). How you define those combinations and sets of data is up to you: e.g. if you want to do the same for milk vs a whole set of other things (coffee, agave syrup, diesel) then loop over those names. Or perhaps use ndgrid to generate all combinations.
Use indexing to select the relevant data, plot, and save (you will find sprintf and fullfile useful). Done.
Answers (1)
Xingwang Yong
on 13 Apr 2021
You could try cell(). You do not have to save the variable names.
c = cell(1,1); % each col contains variables of a sample, each row is a variable in diffent samples
c{1,1}='water';
c{1,2}='oil';
c{1,3}='salt';
for k = 1:numSamples
c{end+1,k} = var1;
c{end+1,k} = var2;
end
2 Comments
Xingwang Yong
on 13 Apr 2021
You can save the cell array then. The key idea here is that you can use index of cell, i.e. c{1,1}, to get variables instead of structure.filed. So you can get rid of the name of samples.
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!