How to store function parameters in a single variable

I would like to store the options for my plot command in a single variable at the beginning of the m-file, so that I can easily change the ploting parameters for all plots before I run the file. My sample code is as follow:
LinesMarker={'-rv'; '-bs'; '-g+';'-c*';'-mh'};
PlotOpt={LinesMarker{x},'DisplayName','FName{x}(1:end-4)','MarkerSize','5','LineWidth','1'}
FName{1}='F1_50Hz_1A.txt'
FName{2}='F1_50Hz_2A.txt'
FName{3}='F2_75Hz_1A.txt'
a=1:1:10;
b{1}=2.*a;
b{2}=3.*a;
b{3}=4.*a;
%
figure
for x = 1:3
plot (a,b{x},PlotOpt);
hold on
legend1 = legend('show','-DynamicLegend');
end
I get the following error:
Error using plot Conversion to double from cell is not possible.
Is there a function or another way to extract a cell with different types (string and numbers) to a list of arguments?
Thanks a lot in advance for any help or tip!

 Accepted Answer

You could use a structure instead.
PlotOpts.LineStyle = '-.';
PlotOpts.Color = [0 1 0];
PlotOpts.Marker = 's';
PlotOpts.MarkerSize = 5;
PlotOpts.LineWidth = 2;
plot(rand(10,1),PlotOpts)
Not that the title and the legend are not lineseries properties so you would have to set them where it corresponds.

6 Comments

Thanks for the fast answer.A Structure might be a solution. But I also wanted to use the indexing variable of the for loop in the options (LinesMarker{x} and FName{x}) so that I can easily change colors and how much of the file name is shown in the legend.
The given file names are juts short examples, I have a lot of sets here from different measurements, where I want then to cut out only certain parts by using the option
FName{x}(StartPos:EndPos).
Is it possible to use variables in the structure? Or maybe it is possible by using the eval function, even if it is recommended to avoid it if possible?
Thanks Again!
Yes, it is possible to use variables. The legend, however, is not a lineseries property and needs to be set elsewhere.
PlotOpts.LineStyle = '-.';
PlotOpts.Color = [0 1 0];
PlotOpts.Marker = 's';
PlotOpts.MarkerSize = 5;
PlotOpts.LineWidth = 2;
legendCell = {'Lookee here','I am much prettier'};
colors = ([0 1 0; 1 0 0]);
aH(1) = subplot(1,2,1);
h(1) = plot(rand(10,1),PlotOpts)
a_marker = '^';
PlotOpts.marker = a_marker;
PlotOpts.Color = colors(2,:);
aH(2) = subplot(1,2,2);
h(2) = plot(rand(10,1),PlotOpts);
for ii = 1:2
legend(aH(ii),legendCell{ii});
end
Sorry, maybe I described my problem and what I want to achieve in a wrong way and not clear.
I can only use the structure for certain properties of the plot function and not in general for arguments I want to pass to an arbitrary function which reads the passed arguments by varargin e.g.
I am also using the "matlab2tikz" function from File Exchange for saving my figures to Latex. What I want is to create a variable at the beginning of my code to set all options and later use this variable when I call the function at different places in my m-file.
Another Example:
% Set my arguments for the function in one variable
PlotOpt={'LinesMarker{x}','DisplayName','FName{x}(1:end-4)','MarkerSize','5','LineWidth','1'}
SaveOptions={'interpretTickLabelsAsTex',true,'width','\figurewidth','height',15};
% some dummy data here, normally data is imported from large measurement files and the filename saved from the import function
a=1:1:10;
b{1}=2.*a;
b{2}=3.*a;
b{3}=4.*a;
FName{1}='F1_50Hz_1A.txt'
FName{2}='F1_50Hz_2A.txt'
FName{3}='F2_75Hz_1A.txt'
% These plotting loops are at several places in the code, only the plotting
% variables form the measurement files change, but all plot commands should
% apply the same Line and Markersettings as well as the same options for
% saving the files.
for x = 1:3
plot (a,b{x},PlotOpt);
matlab2tikz ([FName{x}(1:end-4) '_Output.tex'],SaveOptions);
end
So the goal is to create one variable which contains all arguments for passing to function so that the function can use varargin to read the arguments as they were directly given in the call of the function.
Thanks really a lot for your help!
What you are asking is like mixing apples and oranges, if I understand you correctly. Mixing everything in one place is a job for cell arrays and structures.
"So the goal is to create one variable which contains all arguments for passing to function so that the function can use varargin to read the arguments as they were directly given in the call of the function."
Here is something you could do:
PlotOpts.LineStyle = '-.';
PlotOpts.Color = [0 1 0];
PlotOpts.Marker = 's';
PlotOpts.MarkerSize = 5;
PlotOpts.LineWidth = 2;
your_variable(1) = {PlotOpts}; %Save in cell array
legendCell = {'Lookee here','I am much prettier'};
colors = ([0 1 0; 1 0 0]);
aH(1) = subplot(1,2,1);
h(1) = plot(rand(10,1),your_variable{1}); %Get from cell array
a_marker = '^';
PlotOpts.marker = a_marker;
PlotOpts.Color = colors(2,:);
your_variable(2) = {PlotOpts}; %Save to cell array
aH(2) = subplot(1,2,2);
h(2) = plot(rand(10,1),your:variable{2});%Get from cell array
for ii = 1:2
legend(aH(ii),legendCell{ii});
end
Note that I have never used matlab2tikz so I don't know if you can pass the options as a structure.
In a nutshell, you can save the different options you require in a cell array, or in a structure of structures if you are more comfortable with that.
Thanks again lot for your answer. Saving the structure in a cell array is doing the job for me. I did not know that I have to use round brackets when saving the structure to the cell.
I will mark the problem solved. Thanks a lot!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!