Borrar filtros
Borrar filtros

How can I make the plot function use a string as an argument?

16 visualizaciones (últimos 30 días)
Kevin
Kevin el 7 de Oct. de 2012
Comentada: Stephen23 el 13 de Nov. de 2019
Hi everyone.
I am currently writing a small script which should generate some plots, save them and generate some LaTeX code afterwards to add them to a little report.
What I have is this: I have 11x11x21 matices called: pos_x,pos_y,div_x,div_y ... All in all it's 9 different matrices containing my plot data. Now what I want is I want to plot a specific row, a line and a layer of each and every matrix (at the beginning I specify which row, line and which layer) for every matrix.
All in all, if I specify that at the beginning I end up with 27 Plots (3 Variations*9 Matrices). Now what I did was: I created a script that generates all the necessary plot arguments as strings and puts them inside the cell. Now I made a loop which should simply take the input of the cell, put it as an argument for the plot function BUT this simply does not work.
The lower part of my script looks like this:
for ii =1:3
for kk=1:9
file_name = [variable_names{kk},index_names{ii}];
temp_plot_name1 = ['0:10,',variable_names{kk},'(1:11,index2+1,index3+11)*1000'];
temp_plot_name2 = ['0:10,',variable_names{kk},'(index1+1,1:11,index3+11)*1000'];
temp_plot_name3 = ['-10:10,',variable_names{kk},'(index1+1,index2+1,1:21)*1000'];
temp_plot_names = {temp_plot_name1,temp_plot_name2,temp_plot_name3};
f = figure('visible','off');
plot(temp_plot_names{ii});
grid on;
title(titles{ii});
xlabel(xlabels{ii});
ylable(ylabels{kk});
matlabfrag(file_name);
fprintf(tex_code,['\\psfragfig{D:/Bilder/',file_name,'}\n']);
end
end
Now, the variable_names and index_names are defined above the script. The problem is that Plot now treats the input argument as a string.
So instead of doing this: plot(0:10,pos_x(1:11,index2+1,index3+11)*1000) it does plot('0:10,pos_x(1:11,index2+1,index3+11)*1000') and gives an error.
So after this long description my question is:
How can I make the plot function understand my argument properly?
Kind regards, Kevin
  1 comentario
Stephen23
Stephen23 el 13 de Nov. de 2019
Forcing meta-data into variable names is a sign that you are doing something wrong.
The recommended solution is to store your arrays in one container array (e.g. a cell array or a structure or a table or ...) and then use indexing/fieldnames/etc. to access the data.
Using eval to access variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug (e.g. like that shown in your question).

Iniciar sesión para comentar.

Respuesta aceptada

Razvan
Razvan el 7 de Oct. de 2012
Editada: Razvan el 7 de Oct. de 2012
Example:
V = rand(100,1);
string = 'V(1:10)';
plot(1:10, eval(string))

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by