I am looking to automate some plotting for work and needing to know how I can take a variable string (string changes based upon selection in MATLAB GUI) and then use that to generate a plot.
Example..
function plotting_fcn(bool1, bool2, boo3)
x1 = [1,2,3,4,5];
y1 = [10,20,30,40,50];
y2 = [20,30,40,50,60];
y3 = [30,40,50,60,70];
y4 = [30,40,50,60,70];
str = 'x, [y1';
if(bool1 == 1)
str = strcat(str, ',y2');
end
if(bool2 == 1)
str = strcat(str, ',y3');
end
if(bool3 == 1)
str = strcat(str, ',y4');
end
str = strcat(str, '];');
% HOWEVER I CAN CONVERT str INTO SOMETHING THAT CAN BE PLOTTED.
plot(x, str)
end
If there is another way to do this I am all ears (eyes), but I don't want to have to create plotting code for each of the 3! scenarios that exist for this plot.
Andrew this is just made up code, I apologize for the errors in the code, the concept I am looking to have answered is the ability to generate a string, that can be passed into a function, and then used for plotting. I should have stated the problem better.
There original way is more robust e.g. if I specify idx of 5 your method falls over. There original method also assumed that they always wanted the first y.
This was just a quick example I threw together, the real implementation is much more difficult to bring into this conversation.
My main problem is that I am doing this from a MATLAB GUI and based upon a button press am passing a structure full of parameters. Once these parameters are passed into the function via the struct, they are broken down and then all variables are initialized.
The problem with doing it the way suggested above is that I would need to have those variables already initialized and then create the data variable, which then would be added to my struct and passed in. I would prefer not passing in more data than necessary as these are larger files, but if this is the only way I may be able to make it work.
I just went on what you gave and solved the problem in hand. It's not clear from your description exactly what you are trying to do. I suggest including the actual code and an example of the struct you are passing as an input to the plotting function.
I have a GUI that takes users input, this is one example of input they may provide, and because of the input they can choose to plot all of the thermocouples, or just a selection of them.
function varargout = reporting_GUI(varargin)
function run_btn_Callback(hObject, eventdata, handles)
str = 'x1, [y1';
if(checkbox1 == 1)
str = strcat(str, ',y2');
end
if(checkbox2 == 1)
str = strcat(str, ',y3');
end
if(checkbox3 == 1)
str = strcat(str, ',y4');
end
str = strcat(str, '];');
plt_str(str)
end
function plt_str(str)
x1 = [1,2,3,4,5];
y1 = [10,20,30,40,50];
y2 = [20,30,40,50,60];
y3 = [30,40,50,60,70];
y4 = [40,50,60,70,80];
% HOWEVER I CAN CONVERT str INTO SOMETHING THAT CAN BE PLOTTED.
plot(str)
end
This format allows for several different functions to be created and by simply passing the string the selection of variables can be made in the function that is plotting the data, rather than a LARGE amount of data being passed into the function.