How can I plot using a string in MATLAB?

조회 수: 62 (최근 30일)
Kellen Madsen
Kellen Madsen 2015년 3월 5일
답변: Oran Levi 2021년 4월 26일
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.
Please Help,
Thanks,
Kellen
  댓글 수: 3
Andrew Newell
Andrew Newell 2015년 3월 5일
The variable x isn't defined. Do you mean x1? Also, y3 and y4 are identical. Should they be?
Kellen Madsen
Kellen Madsen 2015년 3월 5일
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.

댓글을 달려면 로그인하십시오.

답변 (3개)

Andrew Newell
Andrew Newell 2015년 3월 5일
편집: Andrew Newell 2015년 3월 5일
It's not necessary to have all those conditional statements. Just use indexing:
function plotting_fcn(idx)
% Note: idx could be Boolean or it could be one or more numbers between 1
% and 4.
x = [1,2,3,4,5];
y = [10,20,30,40,50; ...
20,30,40,50,60; ...
30,40,50,60,70; ...
30,40,50,60,70];
plot(x, y(idx,:))
end
You could plot the second curve using
plotting_fcn([false true false false])
or
plotting_fcn(2)
  댓글 수: 3
Andrew Newell
Andrew Newell 2015년 3월 5일
Strings are still a clumsy way of doing it. Given the revised statement of the problem, I would suggest
function varargout = reporting_GUI(varargin)
function run_btn_Callback(hObject, eventdata, handles)
choices = ([1 checkbox1 checkbox2 checkbox3] == 1);
plt_str(choices)
end
function plt_str(choices)
x1 = [1,2,3,4,5];
y = [10,20,30,40,50; ...
20,30,40,50,60; ...
30,40,50,60,70; ...
40,50,60,70,80];
plot(x1,y(choices,:))
end
Now it's robust. Note that now the first element in choices is always 1, which answers your second objection.
Andrew Newell
Andrew Newell 2015년 3월 5일
For completeness, here is how to do it with a string:
eval(['plot(',str,')'])
But before using that solution, see Why avoid the eval function?

댓글을 달려면 로그인하십시오.


David Barry
David Barry 2015년 3월 5일
function plotting_fcn(bool1, bool2, bool3)
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];
data = y1;
if(bool1 == 1)
data = [data; y2];
end
if(bool2 == 1)
data = [data; y3];
end
if(bool3 == 1)
data = [data; y4];
end
plot(x1, data)
end
  댓글 수: 4
David Barry
David Barry 2015년 3월 5일
편집: David Barry 2015년 3월 5일
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.
Kellen Madsen
Kellen Madsen 2015년 3월 5일
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.

댓글을 달려면 로그인하십시오.


Oran Levi
Oran Levi 2021년 4월 26일
function [text] = lines_intersect(str1,str2)
%UNTITLED4 Summary of this function goes here
% Detailed explanation goes here
x=linspace(-10,10)
plot(x,str1,'r-',x,str2,'g-')
plot(,,'r*')
xlabel('x');
ylabel('y');

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by