"str2mat is not recommended", but what is recommended?

I am looking for the recommended solution for this piece of code. The Matlab documentation says to use char instead of str2mat, but there is no example. Here, paramDisplay is an array of parameter names of varying length, are used as tick marks in a figure.
ticklabels=char(paramDisplay(1));
for paramNum=2:size(paramDisplay,2)
ticklabels= str2mat(ticklabels, char(paramDisplay(paramNum)));
end
set(gca,'XTick',1:1:size(paramDisplay,2),'XTickLabel',ticklabels);

 Accepted Answer

You can just replace str2mat by char and it should work:
for ...
ticklabels = char(ticklabels, char(paramDisplay(paramNum)));
end
If paramDisplay is already a string, you don't need to wrap it into char. You probably don't need the loop either, however I'm puzzled by your loop. The for line implies that that paramDisplay is a 2D array (since you're using size to get the number of columns), but in the body of the loop you're using linear indexing. With linear indexing, you're iterating over the rows first. So it doesn't make sense.
I'm also confused about what paramDisplay is. You use matrix indexing, yet it can be a matrix as that can't be an "array of parameter names". Is it a class?
If paramDisplay were a 1D cell array of strings of varying lengths:
ticklabels = char(paramDisplay{:}) %no need for loop

3 Comments

Hello Guillaume, many thanks for the fast and friendly response. Actually, I didn't write this code, and there are many other places where the Matlab warnings were very clear to me. Now, I am able to remove the str2mat and the warning, but, as you say, it would be good to remove the loop as well. paramDisplay is a 1x11 sym and ticklabels is 11x3 char, meaning that ll strings have the same size of 3 characters. Is this necessary for the set(gca statement?. paramDisplay was created by these 2 lines of code (and renamed when the parameter was passed to a function):
syms x1 x2 x3 x4 x5 x6 x7 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14...
p15 p16 p17 p18 p19 p20 p21 p22 p23 p24 p25 p26 p27
model.Par=[p1, p2, p5, p8, p10, p11,p12, p15, p18, p27, p26];
Now I have the following code:
ticklabels=char(paramDisplay(1));
for paramNum=2:size(paramDisplay,2)
ticklabels= str2mat(ticklabels, char(paramDisplay(paramNum)));
end
testLabels1=char(paramDisplay(1));
for paramNum=2:size(paramDisplay,2)
testLabels1= char(testLabels1, char(paramDisplay(paramNum)));
end
% testLabels2=char(paramDisplay{:});
testLabels3=char(paramDisplay(:));
disp(paramDisplay);
disp(ticklabels);
disp(testLabels1);
% disp(testLabels2);
disp(testLabels3);
set(gca,'XTick',1:1:size(paramDisplay,2),'XTickLabel',ticklabels);
set(gca,'YTick',1:1:tabX)
testLabels2=char(paramDisplay{:}); fails with "Cell contents reference from a non-cell array object." and the disp statements yield
[ p1, p2, p5, p8, p10, p11, p12, p15, p18, p27, p26]
p1
p2
p5
p8
p10
p11
p12
p15
p18
p27
p26
p1
p2
p5
p8
p10
p11
p12
p15
p18
p27
p26
matrix([[p1], [p2], [p5], [p8], [p10], [p11], [p12], [p15], [p18], [p27], [p26]])
Thanks again for the help. Now I have a solution that avoids the loop.
ticklabels=arrayfun(@char,model.Par,'UniformOutput',false);
Sorry, I didn't have access to a computer yesterday.
The arrayfun solution is exactly what I was going to suggest.

Sign in to comment.

More Answers (0)

Categories

Products

Tags

Community Treasure Hunt

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

Start Hunting!