Load data and create loop

I have a data-set. I loaded the data file. There are four columns. I need to use a loop to create four subplots of the data. Each column in the dataset needs one subplot (vs. year, a variable I have already defined). I have put my code below, I have been getting an error: "Undefined variable "data_emission" or class "data_emission.mat"."
% code
load 'data_emission.mat'
x=data_emission.mat{1};
y=data_emission.mat{2};
w=data_emission.mat{3};
z=data_emission.mat{4};
for i = x;y;w;z;
figure
plot(year, i,'--*')
g(i)=subplot(i,1,1);
xlabel('Year','FontSize',12,'FontName','Helvetica')
ylabel('emission (MMT CO2)','FontSize',12,'FontName','Helvetica')
legend(i,'Location','NorthWest','FontSize',12,'FontName','Helvetica')
end
end

1 comentario

Nicolas Schmit
Nicolas Schmit el 4 de Dic. de 2017
It seems that you are trying to access the .mat file itself instead of accessing to the variable stored in the .mat file. Could you please show the contents of the .mat file?

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 4 de Dic. de 2017
Editada: Stephen23 el 4 de Dic. de 2017

0 votos

The problem is caused by incorrect usage of command syntax (the single quotes are interpreted literally). In any case, using command syntax with load is a bad idea, and you would be much better off calling proper function syntax and loading into an output variable:
S = load('data_emission.mat');
x = S.data_emission.mat{1};
y = S.data_emission.mat{2};
... etc
Summary: command syntax is fun for playing around in the command line. Do not use it for any code that you want to be robust and reliable. Also you should always load into an output variable.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 4 de Dic. de 2017

Editada:

el 4 de Dic. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by