MATLAB Answers

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

조회 수: 2(최근 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')

  댓글 수: 0

로그인 to comment.

채택된 답변

Stephen Cobeldick
Stephen Cobeldick 10 Dec 2019
Stephen Cobeldick 님이 편집함. 10 Dec 2019
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

로그인 to comment.

More Answers (0)

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by