我知道strjoin可以用来连接字符串,例如'a', 'b'但是如果其中一个字符串是变量怎么办,例如
a=strcat('file',string(i),'.mat')
而且我要:
strjoin({'rm',a})
当我尝试这样做时,MATLAB 抛出错误,这让我发疯!
Error using strjoin (line 53) First input must be a string array or cell array of character vectors
您使用什么版本的 MATLAB?错误是什么?strjoin 的第一个输入需要是元胞数组。尝试 strjoin({'rm'},a)。
另外,在 17a 之前,执行以下操作:
a = strcat('file', num2str(i),'.mat')
Run Code Online (Sandbox Code Playgroud)
在 >=17a 中执行:
a = "file" + i + ".mat";
Run Code Online (Sandbox Code Playgroud)
这是一个性能比较:
function profFunc
tic;
for i = 1:1E5
a = strcat('file', num2str(i),'.mat');
end
toc;
tic;
for i = 1:1E5
a = "file" + i + ".mat";
end
toc;
end
>> profFunc
Elapsed time is 6.623145 seconds.
Elapsed time is 0.179527 seconds.
Run Code Online (Sandbox Code Playgroud)