Constructing a string with several index requirements
70 次查看(过去 30 天)
显示 更早的评论
Hello, I have a vector of numbers r2 that I need to send over a communciation to an array (called ScanArray). The comms is such that I can only send upto 50 elements in each send (Im using writeline)
Heres the 1st 8 values I need to send
r2 =
0 30 60 90 120 150 180 210
and heres my string that I construct that I send with writeline function
command=['ScanArray(0)(0)=',num2str(r2(1,1)),'ScanArray(0)(1)=',num2str(r2(1,2)),'ScanArray(0)(2)=',num2str(r2(1,3)),'ScanArray(0)(3)=',num2str(r2(1,4))]
Rather than write this out for 50 elements ScanArray(0)(0) -> ScanArray(0)(49), is there a more effcient way to construct this. This was my attemp:
command=[];
for i=1:50
commandnew=['ScanArray(0)(',num2str(i),')=',num2str(r2(1,i))]
command=[command,commandnew]
end
2 个评论
Stephen23
2024-11-8,16:43
"is there a more effcient way to construct this."
Do not fight MATLAB with loops. Use e.g. strings or COMPOSE:
r2 = 0:30:210;
ix = 1:numel(r2);
tx = "ScanArray(0)(" + ix(:) + ")=" + r2(:)
tx = compose('ScanArray(0)(%u) = %u',ix(:),r2(:))
Voss
2024-11-8,16:54
r2 = 0:30:210;
ix = 0:numel(r2)-1;
tx = "ScanArray(0)(" + ix(:) + ")=" + r2(:);
tx = [tx{:}]
tx = compose('ScanArray(0)(%u)=%u',ix(:),r2(:));
tx = [tx{:}]
采纳的回答
Star Strider
2024-11-8,16:10
I’m not certain what you need, however this is one option —
r2 = [0 30 60 90 120 150 180 210];
command=['ScanArray(0)(0)=',num2str(r2(1,1)),'ScanArray(0)(1)=',num2str(r2(1,2)),'ScanArray(0)(2)=',num2str(r2(1,3)),'ScanArray(0)(3)=',num2str(r2(1,4))]
for k = 1:numel(r2)-3
command = ["ScanArray(0)("+(k-1)+")="+r2(1,k)+"ScanArray(0)("+k+")="+r2(1,k+1)+"ScanArray(0)("+(k+1)+")="+r2(1,k+2)+"ScanArray(0)("+(k+2)+")="+r2(1,k+3)]
end
It may be necessary to expand on that, perhaps with a nested loop, for a multi-row ‘r2’.
.
1 个评论
Voss
2024-11-8,17:01
The square brackets are unnecessary when using string concatenation:
"S"+1
["S"+1]
更多回答(3 个)
Voss
2024-11-8,16:12
r2 = [0 30 60 90 120 150 180 210];
command = sprintf('ScanArray(0)(%d)=%g',[0:numel(r2)-1; r2])
The %g is to handle cases where elements of r2 are not integers. If they are always integers you can use %d there instead.
And I don't know but you may need a delimiter between adjacent ScanArray(0) assignments, as in
command = sprintf('ScanArray(0)(%d)=%g ',[0:numel(r2)-1; r2])
or
command = sprintf('ScanArray(0)(%d)=%g;',[0:numel(r2)-1; r2])
etc.
0 个评论
Steven Lord
2024-11-8,16:35
r2 = [0 30 60 90 120 150 180 210];
ind = 0:numel(r2)-1;
result = join("ScanArray(0)(" + ind + ")=" + r2, newline)
Replace newline with ";" to join the substrings with semicolons rather than newlines (which I used so you can easily see the individual substrings included in result.)
4 个评论
Voss
2024-11-8,18:03
Or:
r2 = (0:47)*30;
N = numel(r2);
n = 4;
assert(mod(N,n) == 0)
result = strjoin("ScanArray("+(0:N/n-1)+")("+(0:n-1).'+")="+reshape(r2,n,[]),newline())
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!