Plot by loading a vector, saved from a different workspace as .mat file(converting struct to vector)

3 次查看(过去 30 天)
Hi,
im quite new to Matlab so sorry for the completely amateur question, but I need help.
I have alot of calculations done inside a loop where i change a value and get two vectors(Concentration and Height) that i use to plot.
My objective is to change the value of X from which the two vectors are calulated, from 0,5,10,15,20. And then plot for Concentration and Height corresponding to each of these values.
I though of manually go, and change the value -> run the code->save variable as a .mat file.
Make a new code and define them as a new variable with the load command.
And then make a plot from each of these loaded vectors. But when i load them, the are converted into a 1x1 struct and i get a "Error using plot Invalid data argument".
My question is.
Is there any other way to plot it or can i somehow convert the loaded file from a struct to a vector that i define?
I attached the variables.mat files and this is the code.
Best Regards
Saad Khan
%Code for loading the variables, OBS! Make sure .mat files are in same
%folder as this file!!!
%Loading of variables%
He10=load('He_10.mat');
c10=load('c_10.mat');
He15=load('He_15.mat');
c15=load('c_15.mat');
He20=load('He_20.mat');
c20=load('c_20.mat');
%plot
figure(1)
plot(He10,c10(1:length(He10),1),'-r',He15,c15(1:length(He15),1),'-g',He20,c20(1:length(He20),1),'-b');
xlabel('Height[m]')
ylabel('CO Concentration[vol%]')
legend('O2=10%','O2=15%','O2=20%')
title('CO optimal at different O2 concentrations')
saveas(figure(1),'combo.png')

采纳的回答

Stephen23
Stephen23 2019-12-10
编辑:Stephen23 2019-12-10
Use a loop, e.g.:
D = 'temp'; % path of the folder where the files are saved.
N = [10,15,20];
C = 'rgb';
for k = 1:numel(N)
F = sprintf('He_%d.mat',N(k));
S1 = load(fullfile(D,F));
F = sprintf('c_%d.mat',N(k));
S2 = load(fullfile(D,F));
X = S1.He;
Y = S2.c(1:numel(X));
plot(X,Y,['-*',C(k)])
hold('on')
end
xlabel('Height[m]')
ylabel('CO Concentration[vol%]')
legend('O2=10%','O2=15%','O2=20%')
title('CO optimal at different O2 concentrations')
saveas(figure(1),'combo.png')
Giving (note that your data mostly overlap, but some green and red points are visible too):
combo.png

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by