How to call dynamic variables in loop

I have a txt file and from that i am creating several column vectors and I want to extract range few values out of these column vectors so I am using a for loop and if condition to check the each element in that coumn. After that I am selecting range of rows and creating a new column vector out of it. Now I want to perform some operations on this newly created column vector but the promblem i am facing is how to call the names of these colomun vectors as it contains varibale. below you can find the script i used to create new column vectors
for a=1:count_rows
if Command(a,1)=="-1" && Command(a+1,1)=="0"
c=a;
end
if Command(a,1)=="0" && Command(a+1,1)=="-1"
d=a;
relaxs=['relaxs',num2str(count1)];
relaxv=['relaxv',num2str(count1)];
relaxtime=['relaxtime',num2str(count1)];
relaxcommand=['relaxcommand',num2str(count1)];
assignin('caller',relaxs,S(c:d,1));
assignin('caller',relaxv,V(c:d,1));
assignin('caller',relaxtime,Time(c:d,1));
assignin('caller',relaxcommand,Command(c:d,1));
count1=1+count1;
end

4 件のコメント

Stephen23
Stephen23 2022 年 2 月 14 日
"Now I want to perform some operations on this newly created column vector but the promblem i am facing is how to call the names of these colomun vectors as it contains varibale."
So you designed your data in such a way that it forces you to write complex inefficient code, and now you are struggling to write that complex inefficient code. Why do you need this approach? What are you going to do with those columns?
Is there any particular reason why you can't use simple efficient indexing, which would achieve the same task with less effort?
Shubham Pathare
Shubham Pathare 2022 年 2 月 14 日
Actually I have just started using matlab and dont know many features of it. Final objective is to extract certain range of rows from txt file which satifies the above mentioned criteria.
Walter Roberson
Walter Roberson 2022 年 2 月 14 日
Stephen23
Stephen23 2022 年 2 月 14 日
編集済み: Stephen23 2022 年 2 月 14 日
"Actually I have just started using matlab and dont know many features of it."
Use indexing. Indexing is much simpler and more efficient than what you are attempting.
These tutorials are a good place to learn many basic, important, powerful features of MATLAB (e.g. indexing):
Forcing numbers into variable names like that is a sign that you are doing something wrong:

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

リリース

R2021a

質問済み:

2022 年 2 月 14 日

編集済み:

2022 年 2 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by