How can I plot an order which is a string variable?

조회 수: 1 (최근 30일)
Aurea94
Aurea94 2017년 2월 10일
편집: Stephen23 2017년 2월 10일
Due to my code I have a variable which contains the orders I need to plot: S is a structure of 1x5 struct of 2 fields(Ftime and Ftrans_error)
text = S(1).Ftime,S(1).Ftrans_error,S(2).Ftime,S(2).Ftrans_error,S(3).Ftime,S(3).Ftrans_error
'text' is a string type. I need to make this variable because the size of 'text' changes with the number of variables the structure S contains.
If I do plot(text) I get the following error : Error using plot. Invalid first data argument.
If I write directly
plot(S(1).Ftime,S(1).Ftrans_error,S(2).Ftime,S(2).Ftrans_error,S(3).Ftime,S(3).Ftrans_error) it works properly.
I think the problem is that when I create the variable 'text' it considers I want to plot a string.
So how can I make this 'text' variable an order which plot can execute? I would really apreciate your help. If you need further information to understand the problem please tell me.
Thank you very much in advance!
  댓글 수: 3
Aurea94
Aurea94 2017년 2월 10일
Thank you. I read this and I know that is not the best way, but I cannot do it with a switch becaouse I would need to do the same number of cases as files (there are much more than the 3 I wrote in the exambple to explain you my problem).
I don't understand when you tell me "use the strings to select from fields of a structure of numeric data".
Thanks!
Stephen23
Stephen23 2017년 2월 10일
편집: Stephen23 2017년 2월 10일
@Aurea94: somewhere you have some numeric data. Hopefully not in lots of separate variables (read the link to know why that would be a really bad idea). If you have sensibly kept your numeric data in a structure then you can simply access the fields (and your numeric data) from that structure using those strings:

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

답변 (1개)

Walter Roberson
Walter Roberson 2017년 2월 10일
Please do not name a variable "text" as that interferes with using the key graphics routine text().
What you should be doing is putting your values into a cell array, and then doing cell expansion.
thisorder = {S(1).Ftime,S(1).Ftrans_error, S(2).Ftime, S(2).Ftrans_error, S(3).Ftime,S(3).Ftrans_error};
plot(thisorder{:});
  댓글 수: 1
Aurea94
Aurea94 2017년 2월 10일
Thank you Walter. In fact my variable is named 'text_plot' but I didn't write it to shorten my text. It wasn't a good idea, sorry! I tried what you propose. But I get the same error mentioned before.

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by