what is the best mode to populate this array in this loop annidate?
1 view (last 30 days)
Show older comments
i want to code it to creare text for legend plot! (legend)
it's better to use string and strcat or use arraycell ?
if the better is the second how can i do it?
or is it better to create the text outside the nested loop?
str1="a";
wA=1;
str2="b";
wB=1;
n=5;
b=2;
valA=rand(n,1,1) %it's only for example..i don't use it :)
valB=rand(b,1,1)
clear str
count=0;
strtot="";
for i=1:n
for k=1:b
count=count+1;
if wA==1
strtot=strcat(str1,"-",num2str(valA(i)))
end
if wB==1
strtot=strcat(strtot," ** ",str2,"-",num2str(valB(k)))
end
strArr(count,:)=strtot;
end
end
0 Comments
Answers (1)
Dyuman Joshi
on 11 Sep 2023
It would be better to use string instead of cells as they take less memory and easier to work with -
n=5;
b=2;
valA=rand(n,1,1) %it's only for example..i don't use it :)
valB=rand(b,1,1)
[A,B] = meshgrid(valA,valB);
"a-" + string(A(:)) + " ** b-" + string(B(:))
3 Comments
Dyuman Joshi
on 11 Sep 2023
Edited: Dyuman Joshi
on 11 Sep 2023
I am well aware that your code is different and that you have used wA and wB in your code inside the for loop, but I was giving a general example.
You can always modify the code according to requirement.
Let's consider that the input are -
A = [2 3 5 7];
B = [1 4 6 8];
What should be the output if
1 - wA is zero and wB is one?
2 - wA is one and wB is zero?
3 - wA is one and wB is one?
See Also
Categories
Find more on Characters and Strings 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!