Filter löschen
Filter löschen

Save and load variables of different samples

10 Ansichten (letzte 30 Tage)
Marcel345614
Marcel345614 am 13 Apr. 2021
Kommentiert: Stephen23 am 14 Apr. 2021
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 Kommentare
Marcel345614
Marcel345614 am 14 Apr. 2021
The only idea I came up with is to use something like this:
%%%% Figures of Project %%%%%
%% Figure 1
X1 = strcmpi('milk 10g/l',{A.name});
plot(A(X1).time,A(X1).conductivity)
%% Figure 2: Comparison plot of conductivity in milk for different salt concentrations
X1=strcmpi('milk 0g/l',{A.name});
X2=strcmpi('milk 10g/l',{A.name});
X3=strcmpi('milk 20g/l',{A.name});
plot(A(X1).time,A(X1).conductivity)
plot(A(X2).time,A(X2).conductivity)
plot(A(X3).time,A(X3).conductivity)
%% Figure 3: Comparison of conductivity in different materials
X1=strcmpi('milk 0g/l',{A.name});
X2=strcmpi('apple juice 0g/l',{A.name});
X3=strcmpi('water 0g/l',{A.name});
plot(A(X1).time,A(X1).conductivity)
plot(A(X2).time,A(X2).conductivity)
plot(A(X3).time,A(X3).conductivity)
Is this the most elegant way?
Stephen23
Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Xingwang Yong
Xingwang Yong am 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 Kommentare
Marcel345614
Marcel345614 am 13 Apr. 2021
This is not exactly what I want. The problem is that I apply the first code on different samples. I want to store the resulting variables and load them into another code for plotting.
Xingwang Yong
Xingwang Yong am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by