MATLAB中的字符串索引:单引号与双引号 [英] String indexing in MATLAB: single vs. double quote
问题描述
我有一个字符串矩阵,如下所示:
I have a matrix of strings such as the following:
readFiles = [
"11221", "09";
"11222", "13";
"12821", "06";
"13521", "02";
"13522", "13";
"13711", "05";
"13921", "01";
"14521", ".001";
"15712", ".003"
];
这些用于自动访问某些文件夹和文件.然后,我要执行的操作如下( ii
是一些整数):
These are used to access to some folders and files in an automatic way. Then what I want to do is the following (with ii
being some integer):
FileName = strcat('../../Datasets/hc-1/d',readFiles(ii,1),'/d',...
readFiles(ii,1),readFiles(ii,2),'.dat');
data(ii,:) = LoadBinary(FileName, 6);
然后使用双引号生成字符串 FileName
(我不确定为什么).所以它的值是:
The string FileName
is then generated using double quotes (I'm not sure why). So its value is:
FileName =
"../../Datasets/hc-1/d13921/d1392101.dat"
函数 LoadBinary()
尝试执行以下操作时返回错误:
The function LoadBinary()
returns an error when trying to perform the following operation:
lastdot = strfind(FileName,'.');
FileBase = FileName(1:lastdot(end)-1); % This line
但是,如果我使用单引号手动创建字符串 FileName
,该函数就可以正常工作.
However, if I create the string FileName
manually using single quotes, the function works okay.
简而言之,如果我尝试索引使用上述各行创建的字符串( FileName(1:lastdot(end)-1)
)(导致 FileName ="../../Datasets/hc-1/d13921/d1392101.dat"
),MATLAB返回错误.如果我用单引号( FileName ='../../Datasets/hc-1/d13921/d1392101.dat'
)手动创建,则该函数正常运行.
In a nutshell, if I try to index a string (FileName(1:lastdot(end)-1)
) that is created with the lines above (leading to FileName = "../../Datasets/hc-1/d13921/d1392101.dat"
), MATLAB returns an error. If I create it manually with single quotes (FileName = '../../Datasets/hc-1/d13921/d1392101.dat'
), the function works right.
为什么会这样?有没有解决的方法(即将双引号字符串转换为单引号字符串)?
Why does this happen? Is there a way to fix it (i.e. convert the double-quoted string into a single-quoted one)?
推荐答案
双引号是String数组,而单引号是Char数组.您可以使用函数 char
将字符串数组转换为char类型.所以你会写:
Double quotes are String array, while Single one are Char array. You can convert your string array to a char one using the function char
.
So you'd write :
CharFileName = char(FileName)
它应该可以解决您的问题.
And it should resolve your issue.
这篇关于MATLAB中的字符串索引:单引号与双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!