How can I make the plot function use a string as an argument?
16 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2019 年 11 月 13 日
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).
採用された回答
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Type Identification についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!